Esempio n. 1
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.");
            }
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
0
        //-------------------------------------------------------
        // Get Info via Node
        //-------------------------------------------------------

        /// <summary>
        /// Get one info value by name from the specified database server node.
        /// This method supports user authentication.
        /// </summary>
        /// <param name="node">server node</param>
        /// <param name="name">name of variable to retrieve</param>
        public static string Request(Node node, string name)
        {
            Connection conn = node.GetConnection(DEFAULT_TIMEOUT);

            try
            {
                string response = Info.Request(conn, name);
                node.PutConnection(conn);
                return(response);
            }
            catch (Exception)
            {
                node.CloseConnection(conn);
                throw;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Get default info values from the specified database server node.
        /// This method supports user authentication.
        /// </summary>
        /// <param name="policy">info command configuration parameters, pass in null for defaults</param>
        /// <param name="node">server node</param>
        public static Dictionary <string, string> Request(InfoPolicy policy, Node node)
        {
            int        timeout = (policy == null) ? DEFAULT_TIMEOUT : policy.timeout;
            Connection conn    = node.GetConnection(timeout);

            try
            {
                Dictionary <string, string> result = Request(conn);
                node.PutConnection(conn);
                return(result);
            }
            catch (Exception)
            {
                // Garbage may be in socket.  Do not put back into pool.
                node.CloseConnection(conn);
                throw;
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Get default info values from the specified database server node.
        /// This method supports user authentication.
        /// </summary>
        /// <param name="policy">info command configuration parameters, pass in null for defaults</param>
        /// <param name="node">server node</param>
        public static Dictionary<string, string> Request(InfoPolicy policy, Node node)
        {
            int timeout = (policy == null) ? DEFAULT_TIMEOUT : policy.timeout;
            Connection conn = node.GetConnection(timeout);

            try
            {
                Dictionary<string, string> result = Request(conn);
                node.PutConnection(conn);
                return result;
            }
            catch (Exception)
            {
                // Garbage may be in socket.  Do not put back into pool.
                node.CloseConnection(conn);
                throw;
            }
        }
Esempio n. 6
0
        //-------------------------------------------------------
        // Get Info via Node
        //-------------------------------------------------------
        /// <summary>
        /// Get one info value by name from the specified database server node.
        /// This method supports user authentication.
        /// </summary>
        /// <param name="node">server node</param>
        /// <param name="name">name of variable to retrieve</param>
        public static string Request(Node node, string name)
        {
            Connection conn = node.GetConnection(DEFAULT_TIMEOUT);

            try
            {
                string response = Info.Request(conn, name);
                node.PutConnection(conn);
                return response;
            }
            catch (Exception)
            {
                node.CloseConnection(conn);
                throw;
            }
        }
Esempio n. 7
0
        public void Execute()
        {
            Policy    policy          = GetPolicy();
            int       remainingMillis = policy.timeout;
            DateTime  limit           = DateTime.UtcNow.AddMilliseconds(remainingMillis);
            Node      node            = null;
            Exception exception       = null;
            int       failedNodes     = 0;
            int       failedConns     = 0;
            int       iterations      = 0;

            dataBuffer = ThreadLocalData.GetBuffer();

            // Execute command until successful, timed out or maximum iterations have been reached.
            while (true)
            {
                try
                {
                    node = GetNode();
                    Connection conn = node.GetConnection(remainingMillis);

                    try
                    {
                        // Set command buffer.
                        WriteBuffer();

                        // Reset timeout in send buffer (destined for server) and socket.
                        ByteUtil.IntToBytes((uint)remainingMillis, dataBuffer, 22);

                        // Send command.
                        conn.Write(dataBuffer, dataOffset);

                        // Parse results.
                        ParseResult(conn);

                        // Reflect healthy status.
                        conn.UpdateLastUsed();

                        // Put connection back in pool.
                        node.PutConnection(conn);

                        // Command has completed successfully.  Exit method.
                        return;
                    }
                    catch (AerospikeException ae)
                    {
                        if (ae.KeepConnection())
                        {
                            // Put connection back in pool.
                            conn.UpdateLastUsed();
                            node.PutConnection(conn);
                        }
                        else
                        {
                            // Close socket to flush out possible garbage.  Do not put back in pool.
                            node.CloseConnection(conn);
                        }
                        throw;
                    }
                    catch (SocketException ioe)
                    {
                        node.CloseConnection(conn);

                        if (ioe.ErrorCode == (int)SocketError.TimedOut)
                        {
                            // Full timeout has been reached.  Do not retry.
                            // Close socket to flush out possible garbage.  Do not put back in pool.
                            throw new AerospikeException.Timeout(node, policy.timeout, ++iterations, failedNodes, failedConns);
                        }
                        else
                        {
                            // IO errors are considered temporary anomalies.  Retry.
                            // Close socket to flush out possible garbage.  Do not put back in pool.
                            exception = ioe;
                        }
                    }
                    catch (Exception)
                    {
                        // All runtime exceptions are considered fatal.  Do not retry.
                        // Close socket to flush out possible garbage.  Do not put back in pool.
                        node.CloseConnection(conn);
                        throw;
                    }
                }
                catch (AerospikeException.InvalidNode ine)
                {
                    // Node is currently inactive.  Retry.
                    exception = ine;
                    failedNodes++;
                }
                catch (AerospikeException.Connection ce)
                {
                    // Socket connection error has occurred. Retry.
                    exception = ce;
                    failedConns++;
                }

                if (++iterations > policy.maxRetries)
                {
                    break;
                }

                // Check for client timeout.
                if (policy.timeout > 0)
                {
                    remainingMillis = (int)limit.Subtract(DateTime.UtcNow).TotalMilliseconds - policy.sleepBetweenRetries;

                    if (remainingMillis <= 0)
                    {
                        break;
                    }
                }

                if (policy.sleepBetweenRetries > 0)
                {
                    // Sleep before trying again.
                    Util.Sleep(policy.sleepBetweenRetries);
                }

                // Reset node reference and try again.
                node = null;
            }

            // Retries have been exhausted.  Throw last exception.
            throw exception;
        }
        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)
            {
                node.CloseConnection(conn);
                throw;
            }
        }
Esempio n. 9
0
        public void Execute(Cluster cluster, Policy policy, Key key, Node node, bool isRead)
        {
            Partition          partition = (key != null)? new Partition(key) : null;
            AerospikeException exception = null;
            DateTime           deadline  = DateTime.MinValue;
            int  socketTimeout           = policy.socketTimeout;
            int  totalTimeout            = policy.totalTimeout;
            int  iteration          = 0;
            int  commandSentCounter = 0;
            bool isClientTimeout;

            if (totalTimeout > 0)
            {
                deadline = DateTime.UtcNow.AddMilliseconds(totalTimeout);

                if (socketTimeout == 0 || socketTimeout > totalTimeout)
                {
                    socketTimeout = totalTimeout;
                }
            }

            // Execute command until successful, timed out or maximum iterations have been reached.
            while (true)
            {
                try
                {
                    if (partition != null)
                    {
                        // Single record command node retrieval.
                        node = GetNode(cluster, partition, policy.replica, isRead);
                    }

                    Connection conn = node.GetConnection(socketTimeout);

                    try
                    {
                        // Set command buffer.
                        WriteBuffer();

                        // Check if timeout needs to be changed in send buffer.
                        if (totalTimeout != policy.totalTimeout)
                        {
                            // Reset timeout in send buffer (destined for server) and socket.
                            ByteUtil.IntToBytes((uint)totalTimeout, dataBuffer, 22);
                        }

                        // Send command.
                        conn.Write(dataBuffer, dataOffset);
                        commandSentCounter++;

                        // Parse results.
                        ParseResult(conn);

                        // Put connection back in pool.
                        node.PutConnection(conn);

                        // Command has completed successfully.  Exit method.
                        return;
                    }
                    catch (AerospikeException ae)
                    {
                        if (ae.KeepConnection())
                        {
                            // Put connection back in pool.
                            node.PutConnection(conn);
                        }
                        else
                        {
                            // Close socket to flush out possible garbage.  Do not put back in pool.
                            node.CloseConnection(conn);
                        }

                        if (ae.Result == ResultCode.TIMEOUT)
                        {
                            // Go through retry logic on server timeout.
                            exception       = new AerospikeException.Timeout(node, policy, iteration + 1, false);
                            isClientTimeout = false;

                            if (isRead)
                            {
                                base.sequence++;
                            }
                        }
                        else
                        {
                            ae.SetInDoubt(isRead, commandSentCounter);
                            throw;
                        }
                    }
                    catch (SocketException se)
                    {
                        // Socket errors are considered temporary anomalies.
                        // Retry after closing connection.
                        node.CloseConnection(conn);

                        if (se.SocketErrorCode == SocketError.TimedOut)
                        {
                            isClientTimeout = true;

                            if (isRead)
                            {
                                base.sequence++;
                            }
                        }
                        else
                        {
                            exception       = new AerospikeException(se);
                            isClientTimeout = false;
                            base.sequence++;
                        }
                    }
                    catch (Exception)
                    {
                        // All other exceptions are considered fatal.  Do not retry.
                        // Close socket to flush out possible garbage.  Do not put back in pool.
                        node.CloseConnection(conn);
                        throw;
                    }
                }
                catch (AerospikeException.Connection ce)
                {
                    // Socket connection error has occurred. Retry.
                    exception       = ce;
                    isClientTimeout = false;
                    base.sequence++;
                }

                // Check maxRetries.
                if (++iteration > policy.maxRetries)
                {
                    break;
                }

                if (policy.totalTimeout > 0)
                {
                    // Check for total timeout.
                    long remaining = (long)deadline.Subtract(DateTime.UtcNow).TotalMilliseconds - policy.sleepBetweenRetries;

                    if (remaining <= 0)
                    {
                        break;
                    }

                    if (remaining < totalTimeout)
                    {
                        totalTimeout = (int)remaining;

                        if (socketTimeout > totalTimeout)
                        {
                            socketTimeout = totalTimeout;
                        }
                    }
                }

                if (!isClientTimeout && policy.sleepBetweenRetries > 0)
                {
                    // Sleep before trying again.
                    Util.Sleep(policy.sleepBetweenRetries);
                }
            }

            // Retries have been exhausted.  Throw last exception.
            if (isClientTimeout)
            {
                exception = new AerospikeException.Timeout(node, policy, iteration, true);
            }
            exception.SetInDoubt(isRead, commandSentCounter);
            throw exception;
        }
        public void Execute
        (
            Cluster cluster, Policy policy, Partition partition, Node node, bool isRead,
            int socketTimeout, int totalTimeout, DateTime deadline, int iteration, int commandSentCounter
        )
        {
            AerospikeException exception = null;
            bool isClientTimeout;

            // Execute command until successful, timed out or maximum iterations have been reached.
            while (true)
            {
                if (partition != null)
                {
                    // Single record command node retrieval.
                    try
                    {
                        node = GetNode(cluster, policy, partition, isRead);
                    }
                    catch (AerospikeException ae)
                    {
                        ae.Iteration = iteration;
                        ae.SetInDoubt(isRead, commandSentCounter);
                        throw;
                    }
                }

                try
                {
                    Connection conn = node.GetConnection(socketTimeout);

                    try
                    {
                        // Set command buffer.
                        WriteBuffer();

                        // Check if timeout needs to be changed in send buffer.
                        if (totalTimeout != policy.totalTimeout)
                        {
                            // Reset timeout in send buffer (destined for server) and socket.
                            ByteUtil.IntToBytes((uint)totalTimeout, dataBuffer, 22);
                        }

                        // Send command.
                        conn.Write(dataBuffer, dataOffset);
                        commandSentCounter++;

                        // Parse results.
                        ParseResult(conn);

                        // Put connection back in pool.
                        node.PutConnection(conn);

                        // Command has completed successfully.  Exit method.
                        return;
                    }
                    catch (AerospikeException ae)
                    {
                        if (ae.KeepConnection())
                        {
                            // Put connection back in pool.
                            node.PutConnection(conn);
                        }
                        else
                        {
                            // Close socket to flush out possible garbage.  Do not put back in pool.
                            node.CloseConnection(conn);
                        }

                        if (ae.Result == ResultCode.TIMEOUT)
                        {
                            // Go through retry logic on server timeout.
                            exception       = new AerospikeException.Timeout(policy, false);
                            isClientTimeout = false;
                            ShiftSequenceOnRead(policy, isRead);
                        }
                        else
                        {
                            throw;
                        }
                    }
                    catch (SocketException se)
                    {
                        // Socket errors are considered temporary anomalies.
                        // Retry after closing connection.
                        node.CloseConnection(conn);

                        if (se.SocketErrorCode == SocketError.TimedOut)
                        {
                            isClientTimeout = true;
                            ShiftSequenceOnRead(policy, isRead);
                        }
                        else
                        {
                            exception       = new AerospikeException(se);
                            isClientTimeout = false;
                            base.sequence++;
                        }
                    }
                    catch (Exception)
                    {
                        // All other exceptions are considered fatal.  Do not retry.
                        // Close socket to flush out possible garbage.  Do not put back in pool.
                        node.CloseConnection(conn);
                        throw;
                    }
                }
                catch (SocketException se)
                {
                    // This exception might happen after initial connection succeeded, but
                    // user login failed with a socket error.  Retry.
                    if (se.SocketErrorCode == SocketError.TimedOut)
                    {
                        isClientTimeout = true;
                        ShiftSequenceOnRead(policy, isRead);
                    }
                    else
                    {
                        exception       = new AerospikeException(se);
                        isClientTimeout = false;
                        base.sequence++;
                    }
                }
                catch (AerospikeException.Connection ce)
                {
                    // Socket connection error has occurred. Retry.
                    exception       = ce;
                    isClientTimeout = false;
                    base.sequence++;
                }
                catch (AerospikeException ae)
                {
                    ae.Node      = node;
                    ae.Iteration = iteration;
                    ae.SetInDoubt(isRead, commandSentCounter);
                    throw;
                }

                // Check maxRetries.
                if (iteration > policy.maxRetries)
                {
                    break;
                }

                if (policy.totalTimeout > 0)
                {
                    // Check for total timeout.
                    long remaining = (long)deadline.Subtract(DateTime.UtcNow).TotalMilliseconds - policy.sleepBetweenRetries;

                    if (remaining <= 0)
                    {
                        break;
                    }

                    if (remaining < totalTimeout)
                    {
                        totalTimeout = (int)remaining;

                        if (socketTimeout > totalTimeout)
                        {
                            socketTimeout = totalTimeout;
                        }
                    }
                }

                if (!isClientTimeout && policy.sleepBetweenRetries > 0)
                {
                    // Sleep before trying again.
                    Util.Sleep(policy.sleepBetweenRetries);
                }

                iteration++;

                if (ShouldRetryBatch() && RetryBatch(cluster, socketTimeout, totalTimeout, deadline, iteration, commandSentCounter))
                {
                    // Batch retried in separate commands.  Complete this command.
                    return;
                }
            }

            // Retries have been exhausted.  Throw last exception.
            if (isClientTimeout)
            {
                exception = new AerospikeException.Timeout(policy, true);
            }
            exception.Node      = node;
            exception.Iteration = iteration;
            exception.SetInDoubt(isRead, commandSentCounter);
            throw exception;
        }