public QueryAggregateExecutor(Cluster cluster, QueryPolicy policy, Statement statement)
     : base(cluster, policy, statement)
 {
     inputQueue = new BlockingCollection<object>(500);
     resultSet = new ResultSet(this, policy.recordQueueSize, cancel.Token);
     InitializeThreads();
 }
 public ReadHeaderCommand(Cluster cluster, Policy policy, Key key)
 {
     this.cluster = cluster;
     this.policy = policy;
     this.key = key;
     this.partition = new Partition(key);
 }
 public ExecuteCommand(Cluster cluster, WritePolicy writePolicy, Key key, string packageName, string functionName, Value[] args)
     : base(cluster, writePolicy, key, null)
 {
     this.writePolicy = writePolicy;
     this.packageName = packageName;
     this.functionName = functionName;
     this.args = args;
 }
Пример #4
0
 public ReadCommand(Cluster cluster, Policy policy, Key key, string[] binNames)
 {
     this.cluster = cluster;
     this.policy = policy;
     this.key = key;
     this.partition = new Partition(key);
     this.binNames = binNames;
 }
        /// <summary>
        /// Add node(s) referenced by seed host aliases. In most cases, aliases reference
        /// a single node.  If round robin DNS configuration is used, the seed host may have
        /// several aliases that reference different nodes in the cluster.
        /// </summary>
        public void SeedNodes(Cluster cluster, Host host, List<Node> list)
        {
            IPAddress[] addresses = SetAliases(cluster, host);
            Exception exception = null;
            bool found = false;

            foreach (IPAddress address in addresses)
            {
                try
                {
                    ValidateAlias(cluster, address, host.port);
                    found = true;

                    if (!FindNodeName(list, name))
                    {
                        // New node found.
                        Node node = cluster.CreateNode(this);
                        cluster.AddAliases(node);
                        list.Add(node);
                    }
                    else
                    {
                        // Node already referenced. Close connection.
                        conn.Close();
                    }
                }
                catch (Exception e)
                {
                    // Log and continue to next address.
                    if (Log.DebugEnabled())
                    {
                        Log.Debug("Alias " + address + " failed: " + Util.GetErrorMessage(e));
                    }

                    if (exception == null)
                    {
                        exception = e;
                    }
                }
            }

            if (!found)
            {
                // Exception can't be null here because SetAliases()/Connection.GetHostAddresses()
                // will throw exception if aliases length is zero.
                throw exception;
            }
        }
Пример #6
0
        /// <summary>
        /// Initialize server node with connection parameters.
        /// </summary>
        /// <param name="cluster">collection of active server nodes</param>
        /// <param name="nv">connection parameters</param>
        public Node(Cluster cluster, NodeValidator nv)
        {
            this.cluster = cluster;
            this.name = nv.name;
            this.aliases = nv.aliases;
            this.address = nv.address;
            this.hasDouble = nv.hasDouble;
            this.hasBatchIndex = nv.hasBatchIndex;
            this.hasReplicasAll = nv.hasReplicasAll;

            // Assign host to first IP alias because the server identifies nodes
            // by IP address (not hostname).
            this.host = aliases[0];

            connectionQueue = new BlockingCollection<Connection>(cluster.connectionQueueSize);
        }
        public QueryExecutor(Cluster cluster, QueryPolicy policy, Statement statement)
        {
            this.policy = policy;
            this.statement = statement;
            this.cancel = new CancellationTokenSource();

            this.nodes = cluster.Nodes;

            if (this.nodes.Length == 0)
            {
                throw new AerospikeException(ResultCode.SERVER_NOT_AVAILABLE, "Query failed because cluster is empty.");
            }

            this.threads = new QueryThread[nodes.Length];

            // Initialize maximum number of nodes to query in parallel.
            this.maxConcurrentNodes = (policy.maxConcurrentNodes == 0 || policy.maxConcurrentNodes >= threads.Length) ? threads.Length : policy.maxConcurrentNodes;
        }
Пример #8
0
        public static List<BatchNode> GenerateList(Cluster cluster, BatchPolicy policy, Key[] keys)
        {
            Node[] nodes = cluster.Nodes;

            if (nodes.Length == 0)
            {
                throw new AerospikeException(ResultCode.SERVER_NOT_AVAILABLE, "Command failed because cluster is empty.");
            }

            // Create initial key capacity for each node as average + 25%.
            int keysPerNode = keys.Length / nodes.Length;
            keysPerNode += (int)((uint)keysPerNode >> 2);

            // The minimum key capacity is 10.
            if (keysPerNode < 10)
            {
                keysPerNode = 10;
            }

            // Split keys by server node.
            List<BatchNode> batchNodes = new List<BatchNode>(nodes.Length);

            for (int i = 0; i < keys.Length; i++)
            {
                Partition partition = new Partition(keys[i]);
                Node node = cluster.GetReadNode(partition, policy.replica);
                BatchNode batchNode = FindBatchNode(batchNodes, node);

                if (batchNode == null)
                {
                    batchNodes.Add(new BatchNode(node, keysPerNode, i));
                }
                else
                {
                    batchNode.AddKey(i);
                }
            }
            return batchNodes;
        }
Пример #9
0
        public AsyncBatchExecutor(Cluster cluster, BatchPolicy policy, Key[] keys)
        {
            this.keys = keys;
            this.batchNodes = BatchNode.GenerateList(cluster, policy, keys);

            // Count number of asynchronous commands needed.
            int size = 0;
            foreach (BatchNode batchNode in batchNodes)
            {
                if (batchNode.node.UseNewBatch(policy))
                {
                    // New batch
                    size++;
                }
                else
                {
                    // Old batch only allows one namespace per call.
                    batchNode.SplitByNamespace(keys);
                    size += batchNode.batchNamespaces.Count;
                }
            }
            this.taskSize = size;
        }
Пример #10
0
 public void DropRole(Cluster cluster, AdminPolicy policy, string roleName)
 {
     WriteHeader(DROP_ROLE, 1);
     WriteField(ROLE, roleName);
     ExecuteCommand(cluster, policy);
 }
Пример #11
0
 protected internal override Node GetNode(Cluster cluster)
 {
     return(serverNode);
 }
        public NodeValidator(Cluster cluster, Host host)
        {
            IPAddress[] addresses = Connection.GetHostAddresses(host.name, DEFAULT_TIMEOUT);
            aliases = new Host[addresses.Length];

            for (int i = 0; i < addresses.Length; i++)
            {
                aliases[i] = new Host(addresses[i].ToString(), host.port);
            }

            Exception exception = null;

            for (int i = 0; i < addresses.Length; i++)
            {
                try
                {
                    IPEndPoint address = new IPEndPoint(addresses[i], host.port);
                    Connection conn = new Connection(address, cluster.connectionTimeout);

                    try
                    {
                        if (cluster.user != null)
                        {
                            AdminCommand command = new AdminCommand();
                            command.Authenticate(conn, cluster.user, cluster.password);
                        }
                        Dictionary<string, string> map = Info.Request(conn, "node", "features");
                        string nodeName;

                        if (map.TryGetValue("node", out nodeName))
                        {
                            this.name = nodeName;
                            this.address = address;
                            SetFeatures(map);
                            return;
                        }
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
                catch (Exception e)
                {
                    // Try next address.
                    if (Log.DebugEnabled())
                    {
                        Log.Debug("Alias " + addresses[i] + " failed: " + Util.GetErrorMessage(e));
                    }

                    if (exception == null)
                    {
                        exception = e;
                    }
                }
            }

            if (exception == null)
            {
                throw new AerospikeException.Connection("Failed to find addresses for " + host);
            }
            throw exception;
        }
Пример #13
0
 public User QueryUser(Cluster cluster, AdminPolicy policy, string user)
 {
     base.WriteHeader(QUERY_USERS, 1);
     base.WriteField(USER, user);
     base.ExecuteQuery(cluster, policy);
     return (list.Count > 0) ? list[0] : null;
 }
Пример #14
0
 public Role QueryRole(Cluster cluster, AdminPolicy policy, string roleName)
 {
     base.WriteHeader(QUERY_ROLES, 1);
     base.WriteField(ROLE, roleName);
     base.ExecuteQuery(cluster, policy);
     return (list.Count > 0) ? list[0] : null;
 }
Пример #15
0
        private void ExecuteCommand(Cluster cluster, AdminPolicy policy)
        {
            WriteSize();
            Node node = cluster.GetRandomNode();
            int timeout = (policy == null) ? 1000 : policy.timeout;
            Connection conn = node.GetConnection(timeout);

            try
            {
                conn.Write(dataBuffer, dataOffset);
                conn.ReadFully(dataBuffer, HEADER_SIZE);
                node.PutConnection(conn);
            }
            catch (Exception)
            {
                // Garbage may be in socket.  Do not put back into pool.
                node.CloseConnection(conn);
                throw;
            }

            int result = dataBuffer[RESULT_CODE];

            if (result != 0)
            {
                throw new AerospikeException(result);
            }
        }
Пример #16
0
 public void RevokeRoles(Cluster cluster, AdminPolicy policy, string user, IList<string> roles)
 {
     WriteHeader(REVOKE_ROLES, 2);
     WriteField(USER, user);
     WriteRoles(roles);
     ExecuteCommand(cluster, policy);
 }
Пример #17
0
 public void DropUser(Cluster cluster, AdminPolicy policy, string user)
 {
     WriteHeader(DROP_USER, 1);
     WriteField(USER, user);
     ExecuteCommand(cluster, policy);
 }
        /// <summary>
        /// Initialize Aerospike client with suitable hosts to seed the cluster map.
        /// The client policy is used to set defaults and size internal data structures.
        /// For each host connection that succeeds, the client will:
        /// <list type="bullet">
        /// <item>Add host to the cluster map</item>
        /// <item>Request host's list of other nodes in cluster</item>
        /// <item>Add these nodes to cluster map</item>
        /// </list>
        /// <para>
        /// In most cases, only one host is necessary to seed the cluster. The remaining hosts 
        /// are added as future seeds in case of a complete network failure.
        /// </para>
        /// <para>
        /// If one connection succeeds, the client is ready to process database requests.
        /// If all connections fail and the policy's failIfNotConnected is true, a connection 
        /// exception will be thrown. Otherwise, the cluster will remain in a disconnected state
        /// until the server is activated.
        /// </para>
        /// </summary>
        /// <param name="policy">client configuration parameters, pass in null for defaults</param>
        /// <param name="hosts">array of potential hosts to seed the cluster</param>
        /// <exception cref="AerospikeException">if all host connections fail</exception>
        public AerospikeClient(ClientPolicy policy, params Host[] hosts)
        {
            if (policy == null)
            {
                policy = new ClientPolicy();
            }
            this.readPolicyDefault = policy.readPolicyDefault;
            this.writePolicyDefault = policy.writePolicyDefault;
            this.scanPolicyDefault = policy.scanPolicyDefault;
            this.queryPolicyDefault = policy.queryPolicyDefault;
            this.batchPolicyDefault = policy.batchPolicyDefault;
            this.infoPolicyDefault = policy.infoPolicyDefault;

            cluster = new Cluster(policy, hosts);
            cluster.InitTendThread(policy.failIfNotConnected);
        }
Пример #19
0
 /// <summary>
 /// Initialize task that has already completed.
 /// </summary>
 public BaseTask()
 {
     this.cluster = null;
     this.policy = null;
     this.done = true;
 }
Пример #20
0
 /// <summary>
 /// Initialize task with fields needed to query server nodes.
 /// </summary>
 public BaseTask(Cluster cluster, Policy policy)
 {
     this.cluster = cluster;
     this.policy = new InfoPolicy(policy);
     this.done = false;
 }
Пример #21
0
 public void RevokePrivileges(Cluster cluster, AdminPolicy policy, string roleName, IList<Privilege> privileges)
 {
     WriteHeader(REVOKE_PRIVILEGES, 2);
     WriteField(ROLE, roleName);
     WritePrivileges(privileges);
     ExecuteCommand(cluster, policy);
 }
        public static RegisterTask Register(Cluster cluster, Policy policy, string content, string serverPath, Language language)
        {
            StringBuilder sb = new StringBuilder(serverPath.Length + content.Length + 100);
            sb.Append("udf-put:filename=");
            sb.Append(serverPath);
            sb.Append(";content=");
            sb.Append(content);
            sb.Append(";content-len=");
            sb.Append(content.Length);
            sb.Append(";udf-type=");
            sb.Append(language);
            sb.Append(";");

            // Send UDF to one node. That node will distribute the UDF to other nodes.
            string command = sb.ToString();
            Node node = cluster.GetRandomNode();
            Connection conn = node.GetConnection(policy.timeout);

            try
            {
                Info info = new Info(conn, command);
                Info.NameValueParser parser = info.GetNameValueParser();
                string error = null;
                string file = null;
                string line = null;
                string message = null;

                while (parser.Next())
                {
                    string name = parser.GetName();

                    if (name.Equals("error"))
                    {
                        error = parser.GetValue();
                    }
                    else if (name.Equals("file"))
                    {
                        file = parser.GetValue();
                    }
                    else if (name.Equals("line"))
                    {
                        line = parser.GetValue();
                    }
                    else if (name.Equals("message"))
                    {
                        message = parser.GetStringBase64();
                    }
                }

                if (error != null)
                {
                    throw new AerospikeException("Registration failed: " + error + Environment.NewLine +
                        "File: " + file + Environment.NewLine +
                        "Line: " + line + Environment.NewLine +
                        "Message: " + message
                        );
                }
                node.PutConnection(conn);
                return new RegisterTask(cluster, policy, serverPath);
            }
            catch (Exception)
            {
                conn.Close();
                throw;
            }
        }
Пример #23
0
 public void SetPassword(Cluster cluster, AdminPolicy policy, byte[] user, string password)
 {
     WriteHeader(SET_PASSWORD, 2);
     WriteField(USER, user);
     WriteField(PASSWORD, password);
     ExecuteCommand(cluster, policy);
 }
Пример #24
0
 /// <summary>
 /// Initialize task with fields needed to query server nodes.
 /// </summary>
 public IndexTask(Cluster cluster, Policy policy, string ns, string indexName)
     : base(cluster, policy)
 {
     this.ns = ns;
     this.indexName = indexName;
 }
Пример #25
0
        private void ExecuteQuery(Cluster cluster, AdminPolicy policy)
        {
            WriteSize();
            Node node = cluster.GetRandomNode();
            int timeout = (policy == null) ? 1000 : policy.timeout;
            int status = 0;
            Connection conn = node.GetConnection(timeout);

            try
            {
                conn.Write(dataBuffer, dataOffset);
                status = ReadBlocks(conn);
                node.PutConnection(conn);
            }
            catch (Exception e)
            {
                // Garbage may be in socket.  Do not put back into pool.
                node.CloseConnection(conn);
                throw new AerospikeException(e);
            }

            if (status != QUERY_END && status > 0)
            {
                throw new AerospikeException(status, "Query failed.");
            }
        }
Пример #26
0
 protected internal abstract List <BatchNode> GenerateBatchNodes(Cluster cluster);
Пример #27
0
 public List<Role> QueryRoles(Cluster cluster, AdminPolicy policy)
 {
     base.WriteHeader(QUERY_ROLES, 0);
     base.ExecuteQuery(cluster, policy);
     return list;
 }
Пример #28
0
 public void ChangePassword(Cluster cluster, AdminPolicy policy, byte[] user, string password)
 {
     WriteHeader(CHANGE_PASSWORD, 3);
     WriteField(USER, user);
     WriteField(OLD_PASSWORD, cluster.password);
     WriteField(PASSWORD, password);
     ExecuteCommand(cluster, policy);
 }
Пример #29
0
 public List<User> QueryUsers(Cluster cluster, AdminPolicy policy)
 {
     base.WriteHeader(QUERY_USERS, 0);
     base.ExecuteQuery(cluster, policy);
     return list;
 }
 protected internal override Node GetNode(Cluster cluster)
 {
     return(args.hasWrite ? partition.GetNodeWrite(cluster) : partition.GetNodeRead(cluster));
 }
        public static void Execute(Cluster cluster, BatchPolicy policy, Key[] keys, bool[] existsArray, Record[] records, string[] binNames, int readAttr)
        {
            if (keys.Length == 0)
            {
                return;
            }

            if (policy.allowProleReads)
            {
                // Send all requests to a single node chosen in round-robin fashion in this transaction thread.
                Node node = cluster.GetRandomNode();
                BatchNode batchNode = new BatchNode(node, keys);
                ExecuteNode(batchNode, policy, keys, existsArray, records, binNames, readAttr);
                return;
            }

            List<BatchNode> batchNodes = BatchNode.GenerateList(cluster, policy, keys);

            if (policy.maxConcurrentThreads == 1 || batchNodes.Count <= 1)
            {
                // Run batch requests sequentially in same thread.
                foreach (BatchNode batchNode in batchNodes)
                {
                    ExecuteNode(batchNode, policy, keys, existsArray, records, binNames, readAttr);
                }
            }
            else
            {
                // Run batch requests in parallel in separate threads.
                //
                // Multiple threads write to the record/exists array, so one might think that
                // volatile or memory barriers are needed on the write threads and this read thread.
                // This should not be necessary here because it happens in Executor which does a
                // volatile write (Interlocked.Increment(ref completedCount)) at the end of write threads
                // and a synchronized WaitTillComplete() in this thread.
                Executor executor = new Executor(batchNodes.Count * 2);

                // Initialize threads.
                foreach (BatchNode batchNode in batchNodes)
                {
                    if (batchNode.node.UseNewBatch(policy))
                    {
                        // New batch
                        if (records != null)
                        {
                            MultiCommand command = new BatchGetArrayCommand(batchNode, policy, keys, binNames, records, readAttr);
                            executor.AddCommand(command);
                        }
                        else
                        {
                            MultiCommand command = new BatchExistsArrayCommand(batchNode, policy, keys, existsArray);
                            executor.AddCommand(command);
                        }
                    }
                    else
                    {
                        // There may be multiple threads for a single node because the
                        // wire protocol only allows one namespace per command.  Multiple namespaces
                        // require multiple threads per node.
                        batchNode.SplitByNamespace(keys);

                        foreach (BatchNode.BatchNamespace batchNamespace in batchNode.batchNamespaces)
                        {
                            if (records != null)
                            {
                                MultiCommand command = new BatchGetArrayDirect(batchNode.node, batchNamespace, policy, keys, binNames, records, readAttr);
                                executor.AddCommand(command);
                            }
                            else
                            {
                                MultiCommand command = new BatchExistsArrayDirect(batchNode.node, batchNamespace, policy, keys, existsArray);
                                executor.AddCommand(command);
                            }
                        }
                    }
                }
                executor.Execute(policy.maxConcurrentThreads);
            }
        }
Пример #32
0
 public void CreateUser(Cluster cluster, AdminPolicy policy, string user, string password, IList<string> roles)
 {
     WriteHeader(CREATE_USER, 3);
     WriteField(USER, user);
     WriteField(PASSWORD, password);
     WriteRoles(roles);
     ExecuteCommand(cluster, policy);
 }
Пример #33
0
 public void CreateRole(Cluster cluster, AdminPolicy policy, string roleName, IList<Privilege> privileges)
 {
     WriteHeader(CREATE_ROLE, 2);
     WriteField(ROLE, roleName);
     WritePrivileges(privileges);
     ExecuteCommand(cluster, policy);
 }
Пример #34
0
 protected internal override List <BatchNode> GenerateBatchNodes(Cluster cluster)
 {
     return(BatchNode.GenerateList(cluster, policy, records, sequenceAP, sequenceSC, batch));
 }