예제 #1
0
        private async Task <IBucket> BootstrapBucketAsync(string bucketName, Uri bootstrapUri, BucketType bucketType)
        {
            //Check for any available GC3P connections
            var bootstrapNode = _clusterOptions.GlobalNodes.FirstOrDefault(x =>
                                                                           x.Owner == null && x.EndPoint.Address.Equals(bootstrapUri.GetIpAddress(false)));

            //If not create a new connection but don't add to list until it has an owner
            if (bootstrapNode == null)
            {
                var endPoint = bootstrapUri.GetIpEndPoint(11210, false);
                bootstrapNode = await GetClusterNode(endPoint, bootstrapUri).ConfigureAwait(false);

                //Add to global nodes but its no longer a GC3P node since it has an owner
                if (!_clusterOptions.GlobalNodes.Contains(bootstrapNode))
                {
                    _clusterOptions.GlobalNodes.Add(bootstrapNode);
                }
            }

            BucketBase bucket;

            switch (bucketType)
            {
            case BucketType.Couchbase:
            case BucketType.Ephemeral:
                bucket = new CouchbaseBucket(bucketName, _clusterOptions, _couchbaseContext);
                break;

            case BucketType.Memcached:
                bucket = new MemcachedBucket(bucketName, _clusterOptions, _couchbaseContext);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(bucketType), bucketType, null);
            }

            try
            {
                await bucket.Bootstrap(bootstrapNode).ConfigureAwait(false);
            }
            catch (Exception)
            {
                _couchbaseContext.Unsubscribe(bucket);
                throw;
            }

            _bucketRefs.TryAdd(bucketName, bucket);

            return(bucket);
        }
예제 #2
0
        public async Task <IBucket> BucketAsync(string name)
        {
            if (_bucketRefs.TryGetValue(name, out var bucket))
            {
                return(bucket);
            }

            //No GCCP but we have a connection - not bootstrapped yet so commence strapping das boot
            var bootstrapNodes = _configuration.GlobalNodes.Where(x => x.Owner == null);
            var clusterNodes   = bootstrapNodes as ClusterNode[] ?? bootstrapNodes.ToArray();

            if (clusterNodes.Any())
            {
                //use existing clusterNode from bootstrapping
                bucket = new CouchbaseBucket(name, _configuration, _configContext);
                _configContext.Subscribe((IBucketInternal)bucket);
                await((IBucketInternal)bucket).Bootstrap(clusterNodes.ToArray()).ConfigureAwait(false);

                _bucketRefs.TryAdd(name, bucket);
            }
            else
            {
                //all clusterNodes have owners so create a new one
                var uri           = _configuration.Servers.GetRandom();
                var endPoint      = uri.GetIpEndPoint(11210, false);
                var bootstrapNode = await GetClusterNode(endPoint, uri).ConfigureAwait(false);

                bucket = new CouchbaseBucket(name, _configuration, _configContext);
                _configContext.Subscribe((IBucketInternal)bucket);
                await((IBucketInternal)bucket).Bootstrap(bootstrapNode).ConfigureAwait(false);

                _configuration.GlobalNodes.Add(bootstrapNode);
                _bucketRefs.TryAdd(name, bucket);
            }

            //TODO add 3rd state when connected and have GCCP
            if (bucket == null)
            {
                throw new BucketMissingException(@"{name} Bucket not found!");
            }

            _hasBootStrapped = true;
            return(bucket);
        }
예제 #3
0
        public Cluster(IConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            if (string.IsNullOrWhiteSpace(configuration.Password) || string.IsNullOrWhiteSpace(configuration.UserName))
            {
                throw new ArgumentNullException(nameof(configuration), "Username and password are required.");
            }

            _configuration = configuration;

            if (!_configuration.Buckets.Any())
            {
                _configuration = _configuration.WithBucket("default");
            }

            if (!_configuration.Servers.Any())
            {
                _configuration = _configuration.WithServers("couchbase://localhost");
            }

            var task = Task.Run(async() =>
            {
                foreach (var configBucket in _configuration.Buckets)
                {
                    foreach (var configServer in _configuration.Servers)
                    {
                        var bucket = new CouchbaseBucket(this, configBucket);
                        await bucket.BootstrapAsync(configServer, _configuration).ConfigureAwait(false);
                        _bucketRefs.TryAdd(configBucket, bucket);
                        return;
                    }
                }
            });

            task.ConfigureAwait(false);
            task.Wait();
        }
        private void Connect()
        {
            if (!_configuration.Buckets.Any())
            {
                _configuration = _configuration.WithBucket("default");
            }

            if (!_configuration.Servers.Any())
            {
                _configuration = _configuration.WithServers("couchbase://localhost");
            }

            Task.Run(async() =>
            {
                foreach (var configBucket in _configuration.Buckets)
                {
                    Log.LogDebug("Creating bucket {0}", configBucket);

                    foreach (var configServer in _configuration.Servers)
                    {
                        Log.LogDebug("Bootstrapping bucket {0} using server {1}", configBucket, configServer);

                        var bucket = new CouchbaseBucket(this, configBucket);
                        await bucket.BootstrapAsync(configServer, _configuration).ConfigureAwait(false);
                        _bucketRefs.TryAdd(configBucket, bucket);

                        Log.LogDebug("Succesfully bootstrapped bucket {0} using server {1}", configBucket,
                                     configServer);
                        return;
                    }
                }
            })
            .ConfigureAwait(false)
            .GetAwaiter()
            .GetResult();
        }