コード例 #1
0
        //-------------------------------------------------------
        // Private methods.
        //-------------------------------------------------------

        /// <summary>
        /// Issue request and set results buffer. This method is used internally.
        /// The static request methods should be used instead.
        /// </summary>
        /// <param name="conn">socket connection to server node</param>
        /// <exception cref="AerospikeException">if socket send or receive fails</exception>
        private void SendCommand(Connection conn)
        {
            try
            {
                // Write size field.
                ulong size = ((ulong)offset - 8L) | (2L << 56) | (1L << 48);
                ByteUtil.LongToBytes(size, buffer, 0);

                // Write.
                conn.Write(buffer, offset);

                // Read - reuse input buffer.
                conn.ReadFully(buffer, 8);

                size   = (ulong)ByteUtil.BytesToLong(buffer, 0);
                length = (int)(size & 0xFFFFFFFFFFFFL);
                ResizeBuffer(length);
                conn.ReadFully(buffer, length);
                offset = 0;
            }
            catch (SocketException se)
            {
                throw new AerospikeException(se);
            }
        }
コード例 #2
0
        protected internal override void ParseResult(Connection conn)
        {
            // Read header.
            conn.ReadFully(dataBuffer, MSG_TOTAL_HEADER_SIZE);

            long sz           = ByteUtil.BytesToLong(dataBuffer, 0);
            byte headerLength = dataBuffer[8];
            int  resultCode   = dataBuffer[13];
            int  generation   = ByteUtil.BytesToInt(dataBuffer, 14);
            int  expiration   = ByteUtil.BytesToInt(dataBuffer, 18);
            int  fieldCount   = ByteUtil.BytesToShort(dataBuffer, 26);          // almost certainly 0
            int  opCount      = ByteUtil.BytesToShort(dataBuffer, 28);
            int  receiveSize  = ((int)(sz & 0xFFFFFFFFFFFFL)) - headerLength;

            // Read remaining message bytes.
            if (receiveSize > 0)
            {
                SizeBuffer(receiveSize);
                conn.ReadFully(dataBuffer, receiveSize);
            }

            if (resultCode == 0)
            {
                if (opCount == 0)
                {
                    // Bin data was not returned.
                    record = new Record(null, generation, expiration);
                    return;
                }
                record = ParseRecord(opCount, fieldCount, generation, expiration);
                return;
            }

            if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
            {
                HandleNotFound(resultCode);
                return;
            }

            if (resultCode == ResultCode.FILTERED_OUT)
            {
                if (policy.failOnFilteredOut)
                {
                    throw new AerospikeException(resultCode);
                }
                return;
            }

            if (resultCode == ResultCode.UDF_BAD_RESPONSE)
            {
                record = ParseRecord(opCount, fieldCount, generation, expiration);
                HandleUdfError(resultCode);
                return;
            }

            throw new AerospikeException(resultCode);
        }
コード例 #3
0
        protected internal override void ParseResult(Connection conn)
        {
            // Read header.
            conn.ReadFully(dataBuffer, MSG_TOTAL_HEADER_SIZE);

            int resultCode = dataBuffer[13];

            if (resultCode == 0)
            {
                int generation = ByteUtil.BytesToInt(dataBuffer, 14);
                int expiration = ByteUtil.BytesToInt(dataBuffer, 18);
                record = new Record(null, generation, expiration);
            }
            else
            {
                if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
                {
                    record = null;
                }
                else
                {
                    throw new AerospikeException(resultCode);
                }
            }
            EmptySocket(conn);
        }
コード例 #4
0
        protected internal override void ParseResult(Connection conn)
        {
            // Read header.
            conn.ReadFully(dataBuffer, MSG_TOTAL_HEADER_SIZE);

            int resultCode = dataBuffer[13];

            if (resultCode == 0)
            {
                int generation = ByteUtil.BytesToInt(dataBuffer, 14);
                int expiration = ByteUtil.BytesToInt(dataBuffer, 18);
                record = new Record(null, generation, expiration);
            }
            else
            {
                if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
                {
                    record = null;
                }
                else
                {
                    throw new AerospikeException(resultCode);
                }
            }
            EmptySocket(conn);
        }
コード例 #5
0
        protected internal override void ParseResult(Connection conn)
        {
            // Read header.
            conn.ReadFully(dataBuffer, MSG_TOTAL_HEADER_SIZE);
            conn.UpdateLastUsed();

            int resultCode = dataBuffer[13];

            if (resultCode == 0)
            {
                exists = true;
                return;
            }

            if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
            {
                exists = false;
                return;
            }

            if (resultCode == ResultCode.FILTERED_OUT)
            {
                if (policy.failOnFilteredOut)
                {
                    throw new AerospikeException(resultCode);
                }
                exists = true;
                return;
            }

            throw new AerospikeException(resultCode);
        }
コード例 #6
0
        protected internal override void ParseResult(Connection conn)
        {
            // Read header.
            conn.ReadFully(dataBuffer, MSG_TOTAL_HEADER_SIZE);
            conn.UpdateLastUsed();

            int resultCode = dataBuffer[13];

            if (resultCode == 0)
            {
                int generation = ByteUtil.BytesToInt(dataBuffer, 14);
                int expiration = ByteUtil.BytesToInt(dataBuffer, 18);
                record = new Record(null, generation, expiration);
                return;
            }

            if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
            {
                return;
            }

            if (resultCode == ResultCode.FILTERED_OUT)
            {
                if (policy.failOnFilteredOut)
                {
                    throw new AerospikeException(resultCode);
                }
                return;
            }

            throw new AerospikeException(resultCode);
        }
コード例 #7
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);
            }
        }
コード例 #8
0
        public bool Authenticate(Cluster cluster, Connection conn, byte[] sessionToken)
        {
            dataOffset = 8;
            SetAuthenticate(cluster, sessionToken);
            conn.Write(dataBuffer, dataOffset);
            conn.ReadFully(dataBuffer, HEADER_SIZE);

            return(dataBuffer[RESULT_CODE] == 0);
        }
コード例 #9
0
        public bool Authenticate(Cluster cluster, Connection conn, byte[] sessionToken)
        {
            dataOffset = 8;
            SetAuthenticate(cluster, sessionToken);
            conn.Write(dataBuffer, dataOffset);
            conn.ReadFully(dataBuffer, HEADER_SIZE);

            int result = dataBuffer[RESULT_CODE];

            return(result == 0 || result == ResultCode.SECURITY_NOT_ENABLED);
        }
コード例 #10
0
        protected internal override void ParseResult(Connection conn)
        {
            // Read header.
            conn.ReadFully(dataBuffer, MSG_TOTAL_HEADER_SIZE);

            int resultCode = dataBuffer[13];

            if (resultCode != 0 && resultCode != ResultCode.KEY_NOT_FOUND_ERROR)
            {
                throw new AerospikeException(resultCode);
            }
            exists = resultCode == 0;
        }
コード例 #11
0
        public void Authenticate(Connection conn, byte[] user, byte[] password)
        {
            SetAuthenticate(user, password);
            conn.Write(dataBuffer, dataOffset);
            conn.ReadFully(dataBuffer, HEADER_SIZE);

            int result = dataBuffer[RESULT_CODE];

            if (result != 0)
            {
                throw new AerospikeException(result, "Authentication failed");
            }
        }
コード例 #12
0
        private int ReadBlocks(Connection conn)
        {
            int status = 0;

            while (status == 0)
            {
                conn.ReadFully(dataBuffer, 8);
                long size        = ByteUtil.BytesToLong(dataBuffer, 0);
                int  receiveSize = ((int)(size & 0xFFFFFFFFFFFFL));

                if (receiveSize > 0)
                {
                    if (receiveSize > dataBuffer.Length)
                    {
                        dataBuffer = ThreadLocalData.ResizeBuffer(receiveSize);
                    }
                    conn.ReadFully(dataBuffer, receiveSize);
                    status = ParseBlock(receiveSize);
                }
            }
            return(status);
        }
コード例 #13
0
        protected internal override void ParseResult(Connection conn)
        {
            // Read header.
            conn.ReadFully(dataBuffer, MSG_TOTAL_HEADER_SIZE);

            int resultCode = dataBuffer[13];

            if (resultCode != 0 && resultCode != ResultCode.KEY_NOT_FOUND_ERROR)
            {
                throw new AerospikeException(resultCode);
            }
            exists = resultCode == 0;
            EmptySocket(conn);
        }
コード例 #14
0
        protected internal void EmptySocket(Connection conn)
        {
            // There should not be any more bytes.
            // Empty the socket to be safe.
            long sz           = ByteUtil.BytesToLong(dataBuffer, 0);
            int  headerLength = dataBuffer[8];
            int  receiveSize  = ((int)(sz & 0xFFFFFFFFFFFFL)) - headerLength;

            // Read remaining message bytes.
            if (receiveSize > 0)
            {
                SizeBuffer(receiveSize);
                conn.ReadFully(dataBuffer, receiveSize);
            }
        }
コード例 #15
0
        protected internal override void ParseResult(Connection conn)
        {
            // Read header.
            conn.ReadFully(dataBuffer, MSG_TOTAL_HEADER_SIZE);

            long sz = ByteUtil.BytesToLong(dataBuffer, 0);
            byte headerLength = dataBuffer[8];
            int resultCode = dataBuffer[13];
            int generation = ByteUtil.BytesToInt(dataBuffer, 14);
            int expiration = ByteUtil.BytesToInt(dataBuffer, 18);
            int fieldCount = ByteUtil.BytesToShort(dataBuffer, 26); // almost certainly 0
            int opCount = ByteUtil.BytesToShort(dataBuffer, 28);
            int receiveSize = ((int)(sz & 0xFFFFFFFFFFFFL)) - headerLength;

            // Read remaining message bytes.
            if (receiveSize > 0)
            {
                SizeBuffer(receiveSize);
                conn.ReadFully(dataBuffer, receiveSize);
            }

            if (resultCode != 0)
            {
                if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR || resultCode == ResultCode.LARGE_ITEM_NOT_FOUND)
                {
                    return;
                }

                if (resultCode == ResultCode.UDF_BAD_RESPONSE)
                {
                    record = ParseRecord(opCount, fieldCount, generation, expiration);
                    HandleUdfError(resultCode);
                    return;
                }
                throw new AerospikeException(resultCode);
            }

            if (opCount == 0)
            {
                // Bin data was not returned.
                record = new Record(null, generation, expiration);
                return;
            }
            record = ParseRecord(opCount, fieldCount, generation, expiration);
        }
コード例 #16
0
        public void AuthenticateOld(Cluster cluster, Connection conn)
        {
            dataOffset = 8;
            WriteHeader(AUTHENTICATE, 2);
            WriteField(USER, cluster.user);
            WriteField(CREDENTIAL, cluster.passwordHash);
            WriteSize();

            conn.Write(dataBuffer, dataOffset);
            conn.ReadFully(dataBuffer, HEADER_SIZE);

            int result = dataBuffer[RESULT_CODE];

            if (result != 0)
            {
                throw new AerospikeException(result, "Authentication failed");
            }
        }
コード例 #17
0
        protected internal override void ParseResult(Connection conn)
        {
            // Read header.
            conn.ReadFully(dataBuffer, 8);

            long sz          = ByteUtil.BytesToLong(dataBuffer, 0);
            int  receiveSize = (int)(sz & 0xFFFFFFFFFFFFL);

            if (receiveSize <= 0)
            {
                throw new AerospikeException("Invalid receive size: " + receiveSize);
            }

            SizeBuffer(receiveSize);
            conn.ReadFully(dataBuffer, receiveSize);
            conn.UpdateLastUsed();

            ulong type = (ulong)((sz >> 48) & 0xff);

            if (type == Command.AS_MSG_TYPE)
            {
                dataOffset = 5;
            }
            else if (type == Command.MSG_TYPE_COMPRESSED)
            {
                int    usize = (int)ByteUtil.BytesToLong(dataBuffer, 0);
                byte[] ubuf  = new byte[usize];

                ByteUtil.Decompress(dataBuffer, 8, receiveSize, ubuf, usize);
                dataBuffer = ubuf;
                dataOffset = 13;
            }
            else
            {
                throw new AerospikeException("Invalid proto type: " + type + " Expected: " + Command.AS_MSG_TYPE);
            }

            int resultCode = dataBuffer[dataOffset];

            dataOffset++;
            int generation = ByteUtil.BytesToInt(dataBuffer, dataOffset);

            dataOffset += 4;
            int expiration = ByteUtil.BytesToInt(dataBuffer, dataOffset);

            dataOffset += 8;
            int fieldCount = ByteUtil.BytesToShort(dataBuffer, dataOffset);

            dataOffset += 2;
            int opCount = ByteUtil.BytesToShort(dataBuffer, dataOffset);

            dataOffset += 2;

            if (resultCode == 0)
            {
                if (opCount == 0)
                {
                    // Bin data was not returned.
                    record = new Record(null, generation, expiration);
                    return;
                }
                record = ParseRecord(opCount, fieldCount, generation, expiration);
                return;
            }

            if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
            {
                HandleNotFound(resultCode);
                return;
            }

            if (resultCode == ResultCode.FILTERED_OUT)
            {
                if (policy.failOnFilteredOut)
                {
                    throw new AerospikeException(resultCode);
                }
                return;
            }

            if (resultCode == ResultCode.UDF_BAD_RESPONSE)
            {
                record = ParseRecord(opCount, fieldCount, generation, expiration);
                HandleUdfError(resultCode);
                return;
            }

            throw new AerospikeException(resultCode);
        }
コード例 #18
0
        protected internal void EmptySocket(Connection conn)
        {
            // There should not be any more bytes.
            // Empty the socket to be safe.
            long sz = ByteUtil.BytesToLong(dataBuffer, 0);
            int headerLength = dataBuffer[8];
            int receiveSize = ((int)(sz & 0xFFFFFFFFFFFFL)) - headerLength;

            // Read remaining message bytes.
            if (receiveSize > 0)
            {
                SizeBuffer(receiveSize);
                conn.ReadFully(dataBuffer, receiveSize);
            }
        }
コード例 #19
0
        //-------------------------------------------------------
        // Private methods.
        //-------------------------------------------------------
        /// <summary>
        /// Issue request and set results buffer. This method is used internally.
        /// The static request methods should be used instead.
        /// </summary>
        /// <param name="conn">socket connection to server node</param>
        /// <exception cref="AerospikeException">if socket send or receive fails</exception>
        private void SendCommand(Connection conn)
        {
            try
            {
                // Write size field.
                ulong size = ((ulong)offset - 8L) | (2L << 56) | (1L << 48);
                ByteUtil.LongToBytes(size, buffer, 0);

                // Write.
                conn.Write(buffer, offset);

                // Read - reuse input buffer.
                conn.ReadFully(buffer, 8);

                size = (ulong)ByteUtil.BytesToLong(buffer, 0);
                length = (int)(size & 0xFFFFFFFFFFFFL);
                ResizeBuffer(length);
                conn.ReadFully(buffer, length);
                offset = 0;
            }
            catch (SocketException se)
            {
                throw new AerospikeException(se);
            }
        }
コード例 #20
0
        public void Authenticate(Connection conn, byte[] user, byte[] password)
        {
            SetAuthenticate(user, password);
            conn.Write(dataBuffer, dataOffset);
            conn.ReadFully(dataBuffer, HEADER_SIZE);

            int result = dataBuffer[RESULT_CODE];
            if (result != 0)
            {
                throw new AerospikeException(result, "Authentication failed");
            }
        }
コード例 #21
0
        private int ReadBlocks(Connection conn)
        {
            int status = 0;

            while (status == 0)
            {
                conn.ReadFully(dataBuffer, 8);
                long size = ByteUtil.BytesToLong(dataBuffer, 0);
                int receiveSize = ((int)(size & 0xFFFFFFFFFFFFL));

                if (receiveSize > 0)
                {
                    if (receiveSize > dataBuffer.Length)
                    {
                        dataBuffer = ThreadLocalData.ResizeBuffer(receiveSize);
                    }
                    conn.ReadFully(dataBuffer, receiveSize);
                    status = ParseBlock(receiveSize);
                }
            }
            return status;
        }
コード例 #22
0
        public void Login(Cluster cluster, Connection conn, out byte[] sessionToken, out DateTime?sessionExpiration)
        {
            sessionToken      = null;
            sessionExpiration = null;
            dataOffset        = 8;

            conn.SetTimeout(cluster.loginTimeout);

            try
            {
                if (cluster.authMode == AuthMode.INTERNAL)
                {
                    WriteHeader(LOGIN, 2);
                    WriteField(USER, cluster.user);
                    WriteField(CREDENTIAL, cluster.passwordHash);
                }
                else
                {
                    WriteHeader(LOGIN, 3);
                    WriteField(USER, cluster.user);
                    WriteField(CREDENTIAL, cluster.passwordHash);
                    WriteField(CLEAR_PASSWORD, cluster.password);
                }
                WriteSize();
                conn.Write(dataBuffer, dataOffset);
                conn.ReadFully(dataBuffer, HEADER_SIZE);

                int result = dataBuffer[RESULT_CODE];

                if (result != 0)
                {
                    if (result == INVALID_COMMAND)
                    {
                        // New login not supported.  Try old authentication.
                        AuthenticateOld(cluster, conn);
                        return;
                    }

                    // login failed.
                    throw new AerospikeException(result, "Login failed");
                }

                // Read session token.
                long size        = ByteUtil.BytesToLong(dataBuffer, 0);
                int  receiveSize = ((int)(size & 0xFFFFFFFFFFFFL)) - HEADER_REMAINING;
                int  fieldCount  = dataBuffer[11];

                if (receiveSize <= 0 || receiveSize > dataBuffer.Length || fieldCount <= 0)
                {
                    throw new AerospikeException(result, "Failed to retrieve session token");
                }

                conn.ReadFully(dataBuffer, receiveSize);
                dataOffset = 0;

                for (int i = 0; i < fieldCount; i++)
                {
                    int len = ByteUtil.BytesToInt(dataBuffer, dataOffset);
                    dataOffset += 4;
                    int id = dataBuffer[dataOffset++];
                    len--;

                    if (id == SESSION_TOKEN)
                    {
                        sessionToken = new byte[len];
                        Array.Copy(dataBuffer, dataOffset, sessionToken, 0, len);
                    }
                    else if (id == SESSION_TTL)
                    {
                        // Subtract 60 seconds from ttl so client session expires before server session.
                        long seconds = ByteUtil.BytesToUInt(dataBuffer, dataOffset) - 60;

                        if (seconds > 0)
                        {
                            sessionExpiration = DateTime.UtcNow.AddSeconds(seconds);
                        }
                        else
                        {
                            Log.Warn("Invalid session TTL: " + seconds);
                        }
                    }
                    dataOffset += len;
                }

                if (sessionToken == null)
                {
                    throw new AerospikeException(result, "Failed to retrieve session token");
                }
            }
            finally
            {
                conn.SetTimeout(cluster.connectionTimeout);
            }
        }
コード例 #23
0
        protected internal sealed override void ParseResult(Connection conn)
        {
            // Read blocks of records.  Do not use thread local receive buffer because each
            // block will likely be too big for a cache.  Also, scan callbacks can nest
            // further database commands which would contend with the thread local receive buffer.
            // Instead, use separate heap allocated buffers.
            byte[] protoBuf = new byte[8];
            byte[] buf      = null;
            byte[] ubuf     = null;
            int    receiveSize;

            while (true)
            {
                // Read header
                conn.ReadFully(protoBuf, 8);

                long proto = ByteUtil.BytesToLong(protoBuf, 0);
                int  size  = (int)(proto & 0xFFFFFFFFFFFFL);

                if (size <= 0)
                {
                    continue;
                }

                // Prepare buffer
                if (buf == null || size > buf.Length)
                {
                    // Corrupted data streams can result in a huge length.
                    // Do a sanity check here.
                    if (size > MAX_BUFFER_SIZE)
                    {
                        throw new AerospikeException("Invalid proto size: " + size);
                    }

                    int capacity = (size + 16383) & ~16383;                     // Round up in 16KB increments.
                    buf = new byte[capacity];
                }

                // Read remaining message bytes in group.
                conn.ReadFully(buf, size);
                conn.UpdateLastUsed();

                ulong type = (ulong)((proto >> 48) & 0xff);

                if (type == Command.AS_MSG_TYPE)
                {
                    dataBuffer  = buf;
                    dataOffset  = 0;
                    receiveSize = size;
                }
                else if (type == Command.MSG_TYPE_COMPRESSED)
                {
                    int usize = (int)ByteUtil.BytesToLong(buf, 0);

                    if (ubuf == null || usize > ubuf.Length)
                    {
                        if (usize > MAX_BUFFER_SIZE)
                        {
                            throw new AerospikeException("Invalid proto size: " + usize);
                        }

                        int capacity = (usize + 16383) & ~16383;                         // Round up in 16KB increments.
                        ubuf = new byte[capacity];
                    }

                    ByteUtil.Decompress(buf, 8, size, ubuf, usize);
                    dataBuffer  = ubuf;
                    dataOffset  = 8;
                    receiveSize = usize;
                }
                else
                {
                    throw new AerospikeException("Invalid proto type: " + type + " Expected: " + Command.AS_MSG_TYPE);
                }

                if (!ParseGroup(receiveSize))
                {
                    break;
                }
            }
        }