public CouchbaseCluster(MemcacheClientConfiguration configuration, string bucket, IPEndPoint[] configurationHosts) { if (configurationHosts.Length == 0) { throw new ArgumentException("There should be at least one value in the list", "configurationHosts"); } _isInitialized = false; _configuration = configuration; if (_configuration.Authenticator == null) { _configuration.Authenticator = MemcacheClientConfiguration.SaslPlainAuthenticatorFactory(string.Empty, bucket, string.Empty); } _bucket = bucket; _currentConfigurationHost = 0; _configurationHosts = configurationHosts; _memcacheNodes = new Dictionary <string, IMemcacheNode>(); _locator = new VBucketServerMapLocator(new List <IMemcacheNode>(), new int[][] { }); _connectionTimer = new Timer(_ => ConnectToConfigurationStream(), null, Timeout.Infinite, Timeout.Infinite); _receivedInitialConfigurationBarrier = new ManualResetEventSlim(); }
public void SetUp() { // Create node mocks _nodes = new List <IMemcacheNode>(); for (var i = 0; i < NodeCount; i++) { _nodes.Add(new NodeMock()); } // One server per bucket, using NodeIndex = VBucketIndex % NodeCount _map = new int[VBucketCount][]; for (var i = 0; i < VBucketCount; i++) { _map[i] = new[] { (int)(i % NodeCount) } } ; // Initialize locator _locator = new VBucketServerMapLocator(_nodes, _map); }
/// <summary> /// Handles chunks of data, by parsing them as JSON and updating the current cluster state. /// </summary> /// <param name="chunk">Chunk of data</param> public void HandleConfigurationUpdate(Stream chunk) { var bucket = JsonSerializer.DeserializeFromStream <JsonBucket>(chunk); if (bucket == null) { return; } var vBucketServerMap = bucket.VBucketServerMap; if (vBucketServerMap == null) { return; } // Serialize configuration updates to avoid trouble lock (this) { var updatedNodes = _locator.Nodes; if (vBucketServerMap.ServerList != null) { updatedNodes = GenerateUpdatedNodeList(vBucketServerMap.ServerList); } // Atomic update to the latest cluster state var updatedVBucketMap = vBucketServerMap.VBucketMap ?? _locator.VBucketMap; _locator = new VBucketServerMapLocator(updatedNodes, updatedVBucketMap); // Dispose of the unused nodes after updating the current state if (vBucketServerMap.ServerList != null) { CleanupDeletedNodes(vBucketServerMap.ServerList); } } _receivedInitialConfigurationBarrier.Set(); }