Last active
May 12, 2020 00:45
-
-
Save Aveline67/4429b7631a676beee71384f95e1ad622 to your computer and use it in GitHub Desktop.
ultra minimalistic TiKV client in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Google.Protobuf; | |
using Grpc.Net.Client; | |
using Kvrpcpb; // generated from proto files | |
using System; | |
using System.Threading.Tasks; | |
using Tikvpb; // generated from proto files | |
namespace TiKV | |
{ | |
public static class Test | |
{ | |
// assumes you did that before https://pingcap.com/blog/building-running-and-benchmarking-tikv-and-tidb/ | |
// ultra simple to show how TiKV works. A proper client would be completly different with region caching and catch/retry policies | |
public async static Task Main() | |
{ | |
// as using grpc without https we need this : | |
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true); | |
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); | |
// connect to PD server | |
var channel = GrpcChannel.ForAddress("http://127.0.0.1:2379"); | |
var pdClient = new Pdpb.PD.PDClient(channel); | |
// get PD members | |
var pdMembers = await pdClient.GetMembersAsync(new Pdpb.GetMembersRequest | |
{ | |
Header = new Pdpb.RequestHeader | |
{ | |
} | |
}); | |
// so we can get clusterId for next calls | |
var clusterId = pdMembers.Header.ClusterId; | |
// lets GET and PUT on key "toto" | |
var key = ByteString.CopyFromUtf8("toto"); | |
// first get region | |
var regionResponse = await pdClient.GetRegionAsync(new Pdpb.GetRegionRequest | |
{ | |
RegionKey = key, | |
Header = new Pdpb.RequestHeader | |
{ | |
ClusterId = clusterId, | |
} | |
}); | |
// get leader store for this region | |
var storeResponse = await pdClient.GetStoreAsync(new Pdpb.GetStoreRequest | |
{ | |
StoreId = regionResponse.Leader.StoreId, | |
Header = new Pdpb.RequestHeader | |
{ | |
ClusterId = clusterId, | |
} | |
}); | |
// create a new channel for contacting the store node | |
var channel2 = GrpcChannel.ForAddress($"http://{storeResponse.Store.Address}"); | |
var storeClient = new Tikv.TikvClient(channel2); | |
var putResponse= await client.RawPutAsync(new RawPutRequest | |
{ | |
Context = new Context | |
{ | |
RegionId = regionResponse.Region.Id, | |
Peer = regionResponse.Leader, | |
RegionEpoch = regionResponse.Region.RegionEpoch | |
}, | |
Key = key, | |
Value = ByteString.CopyFromUtf8("cznjcezfczfc") | |
}); | |
// putResponse should not contain errors | |
var getReponse = await storeClient.RawGetAsync(new RawGetRequest | |
{ | |
Context = new Context | |
{ | |
RegionId = regionResponse.Region.Id, | |
Peer = regionResponse.Leader, | |
RegionEpoch = regionResponse.Region.RegionEpoch | |
}, | |
Key = key, | |
}); | |
// result should be "cznjcezfczfc" | |
var result = getReponse.Value.ToStringUtf8(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment