/// <summary> /// Send multiple commands to server and store results. /// This constructor is used internally. /// The static request methods should be used instead. /// </summary> /// <param name="conn">connection to server node</param> /// <param name="commands">commands sent to server</param> public Info(Connection conn, params string[] commands) { buffer = ThreadLocalData.GetBuffer(); // First, do quick conservative buffer size estimate. offset = 8; foreach (string command in commands) { offset += command.Length * 2 + 1; } // If conservative estimate may be exceeded, get exact estimate // to preserve memory and resize buffer. if (offset > buffer.Length) { offset = 8; foreach (string command in commands) { offset += ByteUtil.EstimateSizeUtf8(command) + 1; } ResizeBuffer(offset); } offset = 8; // Skip size field. // The command format is: <name1>\n<name2>\n... foreach (string command in commands) { offset += ByteUtil.StringToUtf8(command, buffer, offset); buffer[offset++] = (byte)'\n'; } SendCommand(conn); }
private void ValidateAlias(Cluster cluster, IPAddress ipAddress, int port) { IPEndPoint address = new IPEndPoint(ipAddress, port); Connection conn = new Connection(address, cluster.connectionTimeout); try { if (cluster.user != null) { AdminCommand command = new AdminCommand(ThreadLocalData.GetBuffer(), 0); 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; this.conn = conn; SetFeatures(map); return; } else { throw new AerospikeException.InvalidNode(); } } catch (Exception) { conn.Close(); throw; } }
// Switch from TLS connection to non-TLS connection. internal SwitchClear(Cluster cluster, Connection conn, byte[] sessionToken) { // Obtain non-TLS addresses. string command = cluster.useServicesAlternate ? "service-clear-alt" : "service-clear-std"; string result = Info.Request(conn, command); List <Host> hosts = Host.ParseServiceHosts(result); Host clearHost; // Find first valid non-TLS host. foreach (Host host in hosts) { try { clearHost = host; string alternativeHost; if (cluster.ipMap != null && cluster.ipMap.TryGetValue(clearHost.name, out alternativeHost)) { clearHost = new Host(alternativeHost, clearHost.port); } IPAddress[] addresses = Connection.GetHostAddresses(clearHost.name, cluster.connectionTimeout); foreach (IPAddress ia in addresses) { try { clearAddress = ia; clearSocketAddress = new IPEndPoint(ia, clearHost.port); clearConn = new Connection(clearSocketAddress, cluster.connectionTimeout, null); try { AdminCommand admin = new AdminCommand(ThreadLocalData.GetBuffer(), 0); if (!admin.Authenticate(cluster, clearConn, sessionToken)) { throw new AerospikeException("Authentication failed"); } return; // Authenticated clear connection. } catch (Exception) { clearConn.Close(); } } catch (Exception) { // Try next address. } } } catch (Exception) { // Try next host. } } throw new AerospikeException("Invalid non-TLS address: " + result); }
/// <summary> /// Get a socket connection from connection pool to the server node. /// </summary> /// <param name="timeoutMillis">connection timeout value in milliseconds if a new connection is created</param> /// <exception cref="AerospikeException">if a connection could not be provided</exception> public Connection GetConnection(int timeoutMillis) { Connection conn; while (connectionQueue.TryTake(out conn)) { if (conn.IsValid()) { try { conn.SetTimeout(timeoutMillis); return(conn); } catch (Exception e) { // Set timeout failed. Something is probably wrong with timeout // value itself, so don't empty queue retrying. Just get out. CloseConnection(conn); throw new AerospikeException.Connection(e); } } CloseConnection(conn); } if (Interlocked.Increment(ref connectionCount) <= cluster.connectionQueueSize) { try { conn = cluster.CreateConnection(host.tlsName, address, timeoutMillis); } catch (Exception) { Interlocked.Decrement(ref connectionCount); throw; } if (cluster.user != null) { try { AdminCommand command = new AdminCommand(ThreadLocalData.GetBuffer(), 0); command.Authenticate(conn, cluster.user, cluster.password); } catch (Exception) { // Socket not authenticated. Do not put back into pool. CloseConnection(conn); throw; } } return(conn); } else { Interlocked.Decrement(ref connectionCount); throw new AerospikeException.Connection(ResultCode.NO_MORE_CONNECTIONS, "Node " + this + " max connections " + cluster.connectionQueueSize + " would be exceeded."); } }
/// <summary> /// Request current status from server node. /// </summary> public void Refresh(Peers peers) { if (!active) { return; } try { if (tendConnection.IsClosed()) { tendConnection = cluster.CreateConnection(host.tlsName, address, cluster.connectionTimeout, null); if (cluster.user != null) { try { AdminCommand command = new AdminCommand(ThreadLocalData.GetBuffer(), 0); command.Authenticate(tendConnection, cluster.user, cluster.password); } catch (Exception) { tendConnection.Close(); throw; } } } if (peers.usePeers) { Dictionary <string, string> infoMap = Info.Request(tendConnection, "node", "peers-generation", "partition-generation"); VerifyNodeName(infoMap); VerifyPeersGeneration(infoMap, peers); VerifyPartitionGeneration(infoMap); } else { string[] commands = cluster.useServicesAlternate ? new string[] { "node", "partition-generation", "services-alternate" } : new string[] { "node", "partition-generation", "services" }; Dictionary <string, string> infoMap = Info.Request(tendConnection, commands); VerifyNodeName(infoMap); VerifyPartitionGeneration(infoMap); AddFriends(infoMap, peers); } peers.refreshCount++; failures = 0; } catch (Exception e) { if (peers.usePeers) { peers.genChanged = true; } RefreshFailed(e); } }
/// <summary> /// Request current status from server node. /// </summary> public void Refresh(Peers peers) { if (!active) { return; } try { if (tendConnection.IsClosed()) { tendConnection = CreateConnection(host.tlsName, address, cluster.connectionTimeout, null); if (cluster.user != null) { if (!EnsureLogin()) { AdminCommand command = new AdminCommand(ThreadLocalData.GetBuffer(), 0); if (!command.Authenticate(cluster, tendConnection, sessionToken)) { // Authentication failed. Session token probably expired. // Must login again to get new session token. command.Login(cluster, tendConnection, out sessionToken, out sessionExpiration); } } } } else { if (cluster.user != null) { EnsureLogin(); } } string[] commands = cluster.rackAware ? INFO_PERIODIC_REB : INFO_PERIODIC; Dictionary <string, string> infoMap = Info.Request(tendConnection, commands); VerifyNodeName(infoMap); VerifyPeersGeneration(infoMap, peers); VerifyPartitionGeneration(infoMap); if (cluster.rackAware) { VerifyRebalanceGeneration(infoMap); } peers.refreshCount++; failures = 0; } catch (Exception e) { peers.genChanged = true; RefreshFailed(e); } }
protected internal sealed override void SizeBuffer() { dataBuffer = ThreadLocalData.GetBuffer(); if (dataOffset > dataBuffer.Length) { dataBuffer = ThreadLocalData.ResizeBuffer(dataOffset); } dataOffset = 0; }
/// <summary> /// Generate unique server hash value from set name, key type and user defined key. /// The hash function is RIPEMD-160 (a 160 bit hash). /// </summary> /// <param name="setName">optional set name, enter null when set does not exist</param> /// <param name="key">record identifier, unique within set</param> /// <returns>unique server hash value</returns> /// <exception cref="AerospikeException">if digest computation fails</exception> public static byte[] ComputeDigest(string setName, Value key) { byte[] buffer = ThreadLocalData.GetBuffer(); int offset = ByteUtil.StringToUtf8(setName, buffer, 0); buffer[offset++] = (byte)key.Type; offset += key.Write(buffer, offset); return(Hash.ComputeHash(buffer, offset)); }
private bool EnsureLogin() { if (performLogin > 0 || (sessionExpiration.HasValue && DateTime.Compare(DateTime.UtcNow, sessionExpiration.Value) >= 0)) { AdminCommand admin = new AdminCommand(ThreadLocalData.GetBuffer(), 0); admin.Login(cluster, tendConnection, out sessionToken, out sessionExpiration); performLogin = 0; return(true); } return(false); }
private void ValidateAlias(Cluster cluster, IPAddress ipAddress, Host alias) { IPEndPoint address = new IPEndPoint(ipAddress, alias.port); Connection conn = cluster.CreateConnection(alias.tlsName, address, cluster.connectionTimeout); try { if (cluster.user != null) { AdminCommand command = new AdminCommand(ThreadLocalData.GetBuffer(), 0); command.Authenticate(conn, cluster.user, cluster.password); } Dictionary <string, string> map; bool hasClusterName = cluster.HasClusterName; if (hasClusterName) { map = Info.Request(conn, "node", "features", "cluster-name"); } else { map = Info.Request(conn, "node", "features"); } string nodeName; if (!map.TryGetValue("node", out nodeName)) { throw new AerospikeException.InvalidNode(); } if (hasClusterName) { string id; if (!map.TryGetValue("cluster-name", out id) || !cluster.clusterName.Equals(id)) { throw new AerospikeException.InvalidNode("Node " + nodeName + ' ' + alias + ' ' + " expected cluster name '" + cluster.clusterName + "' received '" + id + "'"); } } this.name = nodeName; this.primaryHost = alias; this.primaryAddress = address; this.conn = conn; SetFeatures(map); } catch (Exception) { conn.Close(); throw; } }
//------------------------------------------------------- // Constructor //------------------------------------------------------- /// <summary> /// Send single command to server and store results. /// This constructor is used internally. /// The static request methods should be used instead. /// </summary> /// <param name="conn">connection to server node</param> /// <param name="command">command sent to server</param> public Info(Connection conn, string command) { buffer = ThreadLocalData.GetBuffer(); // If conservative estimate may be exceeded, get exact estimate // to preserve memory and resize buffer. if ((command.Length * 2 + 9) > buffer.Length) { offset = ByteUtil.EstimateSizeUtf8(command) + 9; ResizeBuffer(offset); } offset = 8; // Skip size field. // The command format is: <name1>\n<name2>\n... offset += ByteUtil.StringToUtf8(command, buffer, offset); buffer[offset++] = (byte)'\n'; SendCommand(conn); }
private Connection CreateConnection(Pool <Connection> pool) { Connection conn = CreateConnection(host.tlsName, address, cluster.connectionTimeout, pool); if (cluster.user != null) { try { AdminCommand command = new AdminCommand(ThreadLocalData.GetBuffer(), 0); if (!command.Authenticate(cluster, conn, sessionToken)) { throw new AerospikeException("Authentication failed"); } } catch (Exception) { // Socket not authenticated. Do not put back into pool. CloseConnection(conn); throw; } } return(conn); }
public AdminCommand() { dataBuffer = ThreadLocalData.GetBuffer(); dataOffset = 8; }
/// <summary> /// Send default empty command to server and store results. /// This constructor is used internally. /// The static request methods should be used instead. /// </summary> /// <param name="conn">connection to server node</param> public Info(Connection conn) { buffer = ThreadLocalData.GetBuffer(); offset = 8; // Skip size field. SendCommand(conn); }
private void SetAddress(Cluster cluster, Dictionary <string, string> map, string addressCommand, string tlsName) { string result = map[addressCommand]; List <Host> hosts = Host.ParseServiceHosts(result); Host h; // Search real hosts for seed. foreach (Host host in hosts) { h = host; string alt; if (cluster.ipMap != null && cluster.ipMap.TryGetValue(h.name, out alt)) { h = new Host(alt, h.port); } if (h.Equals(this.primaryHost)) { // Found seed which is not a load balancer. return; } } // Seed not found, so seed is probably a load balancer. // Find first valid real host. foreach (Host host in hosts) { try { h = host; string alt; if (cluster.ipMap != null && cluster.ipMap.TryGetValue(h.name, out alt)) { h = new Host(alt, h.port); } IPAddress[] addresses = Connection.GetHostAddresses(h.name, cluster.connectionTimeout); foreach (IPAddress address in addresses) { try { IPEndPoint socketAddress = new IPEndPoint(address, h.port); Connection conn = (cluster.tlsPolicy != null) ? new TlsConnection(cluster.tlsPolicy, tlsName, socketAddress, cluster.connectionTimeout, cluster.maxSocketIdleMillis, null) : new Connection(socketAddress, cluster.connectionTimeout, cluster.maxSocketIdleMillis, null); try { if (cluster.user != null) { AdminCommand admin = new AdminCommand(ThreadLocalData.GetBuffer(), 0); if (!admin.Authenticate(cluster, conn, this.sessionToken)) { throw new AerospikeException("Authentication failed"); } } // Authenticated connection. Set real host. SetAliases(addresses, tlsName, h.port); this.primaryHost = new Host(address.ToString(), tlsName, h.port); this.primaryAddress = socketAddress; this.primaryConn.Close(); this.primaryConn = conn; return; } catch (Exception) { conn.Close(); } } catch (Exception) { // Try next address. } } } catch (Exception) { // Try next host. } } // Failed to find a valid host. throw new AerospikeException("Invalid address: " + result); }
private void SetAddress(Cluster cluster, Dictionary <string, string> map, string addressCommand, string tlsName) { string result; if (!map.TryGetValue(addressCommand, out result) || result == null || result.Length == 0) { // Server does not support service level call (service-clear-std, ...). // Load balancer detection is not possible. return; } List <Host> hosts = Host.ParseServiceHosts(result); Host h; // Search real hosts for seed. foreach (Host host in hosts) { h = host; string alt; if (cluster.ipMap != null && cluster.ipMap.TryGetValue(h.name, out alt)) { h = new Host(alt, h.port); } if (h.Equals(this.primaryHost)) { // Found seed which is not a load balancer. return; } } // Seed not found, so seed is probably a load balancer. // Find first valid real host. foreach (Host host in hosts) { try { h = host; string alt; if (cluster.ipMap != null && cluster.ipMap.TryGetValue(h.name, out alt)) { h = new Host(alt, h.port); } IPAddress[] addresses = Connection.GetHostAddresses(h.name, cluster.connectionTimeout); foreach (IPAddress address in addresses) { try { IPEndPoint socketAddress = new IPEndPoint(address, h.port); Connection conn = (cluster.tlsPolicy != null) ? new TlsConnection(cluster.tlsPolicy, tlsName, socketAddress, cluster.connectionTimeout, null) : new Connection(socketAddress, cluster.connectionTimeout, null); try { if (cluster.user != null) { AdminCommand admin = new AdminCommand(ThreadLocalData.GetBuffer(), 0); if (!admin.Authenticate(cluster, conn, this.sessionToken)) { throw new AerospikeException("Authentication failed"); } } // Authenticated connection. Set real host. SetAliases(address, tlsName, h.port); this.primaryHost = new Host(address.ToString(), tlsName, h.port); this.primaryAddress = socketAddress; this.primaryConn.Close(); this.primaryConn = conn; return; } catch (Exception) { conn.Close(); } } catch (Exception) { // Try next address. } } } catch (Exception) { // Try next host. } } // Failed to find a valid address. IP Address is probably internal on the cloud // because the server access-address is not configured. Log warning and continue // with original seed. if (Log.InfoEnabled()) { Log.Info("Invalid address " + result + ". access-address is probably not configured on server."); } }
public Packer() { this.buffer = ThreadLocalData.GetBuffer(); }
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; }
/// <summary> /// Get a socket connection from connection pool to the server node. /// </summary> /// <param name="timeoutMillis">connection timeout value in milliseconds if a new connection is created</param> /// <exception cref="AerospikeException">if a connection could not be provided</exception> public Connection GetConnection(int timeoutMillis) { uint max = (uint)cluster.connPoolsPerNode; uint initialIndex; bool backward; if (max == 1) { initialIndex = 0; backward = false; } else { uint iter = connectionIter++; // not atomic by design initialIndex = iter % max; backward = true; } Pool <Connection> pool = connectionPools[initialIndex]; uint queueIndex = initialIndex; Connection conn; while (true) { if (pool.TryDequeue(out conn)) { // Found socket. // Verify that socket is active and receive buffer is empty. if (cluster.IsConnCurrentTran(conn.LastUsed) && conn.IsValid()) { try { conn.SetTimeout(timeoutMillis); return(conn); } catch (Exception e) { // Set timeout failed. Something is probably wrong with timeout // value itself, so don't empty queue retrying. Just get out. CloseConnection(conn); throw new AerospikeException.Connection(e); } } CloseConnection(conn); } else if (pool.IncrementTotal() <= pool.Capacity) { // Socket not found and queue has available slot. // Create new connection. try { conn = CreateConnection(host.tlsName, address, timeoutMillis, pool); } catch (Exception) { pool.DecrementTotal(); throw; } if (cluster.user != null) { try { AdminCommand command = new AdminCommand(ThreadLocalData.GetBuffer(), 0); if (!command.Authenticate(cluster, conn, sessionToken)) { SignalLogin(); throw new AerospikeException("Authentication failed"); } } catch (Exception) { // Socket not authenticated. Do not put back into pool. CloseConnection(conn); throw; } } return(conn); } else { // Socket not found and queue is full. Try another queue. pool.DecrementTotal(); if (backward) { if (queueIndex > 0) { queueIndex--; } else { queueIndex = initialIndex; if (++queueIndex >= max) { break; } backward = false; } } else if (++queueIndex >= max) { break; } pool = connectionPools[queueIndex]; } } throw new AerospikeException.Connection(ResultCode.NO_MORE_CONNECTIONS, "Node " + this + " max connections " + cluster.maxConnsPerNode + " would be exceeded."); }
private void ValidateAddress(Cluster cluster, IPAddress address, string tlsName, int port, bool detectLoadBalancer) { IPEndPoint socketAddress = new IPEndPoint(address, port); Connection conn = (cluster.tlsPolicy != null) ? new TlsConnection(cluster.tlsPolicy, tlsName, socketAddress, cluster.connectionTimeout, null) : new Connection(socketAddress, cluster.connectionTimeout, null); try { if (cluster.user != null) { // Login AdminCommand admin = new AdminCommand(ThreadLocalData.GetBuffer(), 0); admin.Login(cluster, conn, out sessionToken, out sessionExpiration); if (cluster.tlsPolicy != null && cluster.tlsPolicy.forLoginOnly) { // Switch to using non-TLS socket. SwitchClear sc = new SwitchClear(cluster, conn, sessionToken); conn.Close(); address = sc.clearAddress; socketAddress = sc.clearSocketAddress; conn = sc.clearConn; // Disable load balancer detection since non-TLS address has already // been retrieved via service info command. detectLoadBalancer = false; } } List <string> commands = new List <string>(5); commands.Add("node"); commands.Add("partition-generation"); commands.Add("features"); bool hasClusterName = cluster.HasClusterName; if (hasClusterName) { commands.Add("cluster-name"); } string addressCommand = null; if (detectLoadBalancer) { // Seed may be load balancer with changing address. Determine real address. addressCommand = (cluster.tlsPolicy != null) ? cluster.useServicesAlternate ? "service-tls-alt" : "service-tls-std" : cluster.useServicesAlternate ? "service-clear-alt" : "service-clear-std"; commands.Add(addressCommand); } // Issue commands. Dictionary <string, string> map = Info.Request(conn, commands); // Node returned results. this.primaryHost = new Host(address.ToString(), tlsName, port); this.primaryAddress = socketAddress; this.primaryConn = conn; ValidateNode(map); ValidatePartitionGeneration(map); SetFeatures(map); if (hasClusterName) { ValidateClusterName(cluster, map); } if (addressCommand != null) { SetAddress(cluster, map, addressCommand, tlsName); } } catch (Exception) { conn.Close(); throw; } }