internal bool RefreshHosts() { lock (this) { try { if (!_isDiconnected) { RefreshNodeListAndTokenMap(); return(true); } return(false); } catch (NoHostAvailableException) { _logger.Error("ControlConnection is lost now."); return(false); } catch (Exception ex) { if (CassandraConnection.IsStreamRelatedException(ex)) { _logger.Error("ControlConnection is lost now."); return(false); } else { _logger.Error("Unexpected error occurred during forced ControlConnection refresh.", ex); throw; } } } }
void ExecConn(RequestHandler handler, bool moveNext) { while (true) { try { int streamId; handler.Connect(this, moveNext, out streamId); handler.Begin(this, streamId); return; } catch (Exception ex) { if (!CassandraConnection.IsStreamRelatedException(ex)) { handler.Complete(this, null, ex); return; } else if (_alreadyDisposed.IsTaken()) { return; } } } }
void ClbNoQuery(IAsyncResult ar) { var token = ar.AsyncState as LongToken; try { try { object value; token.Process(this, ar, out value); token.Complete(this, value); } catch (QueryValidationException exc) { var decision = GetRetryDecision(token.Query, exc, _policies.RetryPolicy, token.QueryRetries); if (decision == null) { token.InnerExceptions[token.Connection.GetHostAdress()] = exc; ExecConn(token, true); } else { switch (decision.DecisionType) { case RetryDecision.RetryDecisionType.Rethrow: token.Complete(this, null, exc); return; case RetryDecision.RetryDecisionType.Retry: token.Consistency = decision.RetryConsistencyLevel ?? token.Consistency; token.QueryRetries++; token.InnerExceptions[token.Connection.GetHostAdress()] = exc; ExecConn(token, false); return; default: token.Complete(this, null); return; } } } } catch (Exception ex) { if (CassandraConnection.IsStreamRelatedException(ex)) { token.InnerExceptions[token.Connection.GetHostAdress()] = ex; ExecConn(token, true); } else { token.Complete(this, null, ex); } } }
private void SetupControlConnection(bool refreshOnly = false) { lock (this) { try { _reconnectionTimer.Change(Timeout.Infinite, Timeout.Infinite); _logger.Info("Refreshing ControlConnection..."); if (!refreshOnly) { Monitor.Exit(this); try { SetupEventListener(); } finally { Monitor.Enter(this); } } RefreshNodeListAndTokenMap(); _isDiconnected = false; _logger.Info("ControlConnection is fresh!"); } catch (NoHostAvailableException) { _isDiconnected = true; if (!shotDown.IsTaken()) { _logger.Error("ControlConnection is lost. Retrying.."); _reconnectionTimer.Change(_reconnectionSchedule.NextDelayMs(), Timeout.Infinite); } } catch (Exception ex) { _isDiconnected = true; if (CassandraConnection.IsStreamRelatedException(ex)) { if (!shotDown.IsTaken()) { _logger.Error("ControlConnection is lost. Retrying.."); _reconnectionTimer.Change(_reconnectionSchedule.NextDelayMs(), Timeout.Infinite); } } else { _logger.Error("Unexpected error occurred during ControlConnection refresh.", ex); // throw; } } } }
CassandraConnection AllocateConnection(IPAddress endPoint, out Exception outExc) { CassandraConnection nconn = null; outExc = null; try { nconn = new CassandraConnection(this, endPoint, _protocolOptions, _socketOptions, _clientOptions, _authProvider); var streamId = nconn.AllocateStreamId(); var options = ProcessExecuteOptions(nconn.ExecuteOptions(streamId)); if (!string.IsNullOrEmpty(_keyspace)) { nconn.SetKeyspace(_keyspace); } } catch (Exception ex) { if (nconn != null) { nconn.Dispose(); } if (CassandraConnection.IsStreamRelatedException(ex)) { HostIsDown(endPoint); outExc = ex; return(null); } else { throw ex; } } _logger.Info("Allocated new connection"); return(nconn); }
void ExecConn(LongToken token, bool moveNext) { while (true) { try { token.Connect(this, moveNext); token.Begin(this); return; } catch (Exception ex) { if (!CassandraConnection.IsStreamRelatedException(ex)) { token.Complete(this, null, ex); return; } //else //retry } } }
void ClbNoQuery(IAsyncResult ar) { var token = ar.AsyncState as LongToken; try { try { object value; token.Process(this, ar, out value); token.Complete(this, value); } catch (QueryValidationException exc) { var decision = GetRetryDecision(token.Query, exc, token.Query != null ? (token.Query.RetryPolicy ?? _policies.RetryPolicy) : _policies.RetryPolicy, token.QueryRetries); if (decision == null) { if (!token.InnerExceptions.ContainsKey(token.Connection.GetHostAdress())) { token.InnerExceptions.Add(token.Connection.GetHostAdress(), new List <Exception>()); } token.InnerExceptions[token.Connection.GetHostAdress()].Add(exc); ExecConn(token, true); } else { switch (decision.DecisionType) { case RetryDecision.RetryDecisionType.Rethrow: token.Complete(this, null, exc); return; case RetryDecision.RetryDecisionType.Retry: if (token.LongActionAc.IsCompleted) { return; } token.Consistency = decision.RetryConsistencyLevel.HasValue ? decision.RetryConsistencyLevel.Value : token.Consistency; token.QueryRetries++; if (!token.InnerExceptions.ContainsKey(token.Connection.GetHostAdress())) { token.InnerExceptions.Add(token.Connection.GetHostAdress(), new List <Exception>()); } token.InnerExceptions[token.Connection.GetHostAdress()].Add(exc); ExecConn(token, false); return; default: token.Complete(this, null); return; } } } } catch (Exception ex) { if (CassandraConnection.IsStreamRelatedException(ex)) { if (!token.InnerExceptions.ContainsKey(token.Connection.GetHostAdress())) { token.InnerExceptions.Add(token.Connection.GetHostAdress(), new List <Exception>()); } token.InnerExceptions[token.Connection.GetHostAdress()].Add(ex); ExecConn(token, true); } else { token.Complete(this, null, ex); } } }
internal void RequestCallback(IAsyncResult ar) { var handler = ar.AsyncState as RequestHandler; try { try { object value; handler.Process(this, ar, out value); handler.Complete(this, value); } catch (QueryValidationException exc) { var decision = GetRetryDecision(handler.Statement, exc, handler.Statement != null ? (handler.Statement.RetryPolicy ?? Policies.RetryPolicy) : Policies.RetryPolicy, handler.QueryRetries); if (decision == null) { if (!handler.InnerExceptions.ContainsKey(handler.Connection.GetHostAdress())) { handler.InnerExceptions.Add(handler.Connection.GetHostAdress(), new List <Exception>()); } handler.InnerExceptions[handler.Connection.GetHostAdress()].Add(exc); ExecConn(handler, true); } else { switch (decision.DecisionType) { case RetryDecision.RetryDecisionType.Rethrow: handler.Complete(this, null, exc); return; case RetryDecision.RetryDecisionType.Retry: if (handler.LongActionAc.IsCompleted) { return; } handler.Consistency = (decision.RetryConsistencyLevel.HasValue && (decision.RetryConsistencyLevel.Value < ConsistencyLevel.Serial)) ? decision.RetryConsistencyLevel.Value : handler.Consistency; handler.QueryRetries++; if (!handler.InnerExceptions.ContainsKey(handler.Connection.GetHostAdress())) { handler.InnerExceptions.Add(handler.Connection.GetHostAdress(), new List <Exception>()); } handler.InnerExceptions[handler.Connection.GetHostAdress()].Add(exc); ExecConn(handler, exc is UnavailableException); return; default: handler.Complete(this, null); return; } } } } catch (Exception ex) { if (CassandraConnection.IsStreamRelatedException(ex)) { if (!handler.InnerExceptions.ContainsKey(handler.Connection.GetHostAdress())) { handler.InnerExceptions.Add(handler.Connection.GetHostAdress(), new List <Exception>()); } handler.InnerExceptions[handler.Connection.GetHostAdress()].Add(ex); ExecConn(handler, true); } else { handler.Complete(this, null, ex); } } }
CassandraConnection AllocateConnection(IPAddress endPoint, HostDistance hostDistance, out Exception outExc) { CassandraConnection nconn = null; outExc = null; try { int no = 1; if (!_allocatedConnections.TryAdd(endPoint, new AtomicValue <int>(1))) { AtomicValue <int> val; _allocatedConnections.TryGetValue(endPoint, out val); no = Interlocked.Increment(ref val.RawValue); if (no > _poolingOptions.GetMaxConnectionPerHost(hostDistance)) { Interlocked.Decrement(ref val.RawValue); outExc = new ToManyConnectionsPerHost(); return(null); } } RETRY: nconn = new CassandraConnection(this, endPoint, _protocolOptions, _socketOptions, _clientOptions, _authProvider, _authInfoProvider, _binaryProtocolVersion); var streamId = nconn.AllocateStreamId(); try { var options = ProcessExecuteOptions(nconn.ExecuteOptions(streamId)); } catch (CassandraConnectionBadProtocolVersionException) { if (_binaryProtocolVersion == 1) { throw; } else { _binaryProtocolVersion = 1; goto RETRY; } } if (!string.IsNullOrEmpty(_keyspace)) { nconn.SetKeyspace(_keyspace); } } catch (Exception ex) { if (nconn != null) { nconn.Dispose(); nconn = null; } AtomicValue <int> val; _allocatedConnections.TryGetValue(endPoint, out val); Interlocked.Decrement(ref val.RawValue); if (CassandraConnection.IsStreamRelatedException(ex)) { HostIsDown(endPoint); outExc = ex; return(null); } else { throw ex; } } _logger.Info("Allocated new connection"); return(nconn); }