Use this when the insert fails even before the request is queued by the InternalConnectionHandler
322035
public void reportFailure(String importerName, String procName, boolean decrementPending) {
        StatsInfo statsInfo = getStatsInfo(importerName, procName);
        if (decrementPending) {
            statsInfo.m_pendingCount.decrementAndGet();
        }
        statsInfo.m_failureCount.incrementAndGet();
    }
1069728
def _handle_tasks_insert(self, batch_size=None):
        """"""
        if self._tasks_inserted:
            raise errors.ContextAlreadyStartedError(
                "This Context has already had its tasks inserted.")

        task_map = self._get_tasks_by_queue()

        # QUESTION: Should the persist happen before or after the task
        # insertion?  I feel like this is something that will alter the
        # behavior of the tasks themselves by adding a callback (check context
        # complete) to each Async's callback stack.

        # If we are able to and there is a reason to persist... persist.
        callbacks = self._options.get('callbacks')
        if self._persistence_engine and callbacks:
            self.persist()

        retry_transient = self._options.get('retry_transient_errors', True)
        retry_delay = self._options.get('retry_delay', RETRY_SLEEP_SECS)

        for queue, tasks in task_map.iteritems():
            for batch in _task_batcher(tasks, batch_size=batch_size):
                inserted = self._insert_tasks(
                    batch, queue=queue, retry_transient_errors=retry_transient,
                    retry_delay=retry_delay
                )
                if isinstance(inserted, (int, long)):
                    # Don't blow up on insert_tasks that don't return counts.
                    self._insert_success_count += inserted
                    self._insert_failed_count += len(batch) - inserted
231907
protected void insertBatch(Content[] batch, int id) 
    throws IOException {
        retry = 0;
        sleepTime = 500;
        while (retry < maxRetries) {
            try {
                if (retry == 1) { 
                    LOG.info("Retrying document insert");
                }
                List<RequestException> errors = 
                    sessions[id].insertContentCollectErrors(batch);
                if (errors != null) {
                    for (RequestException ex : errors) {
                        Throwable cause = ex.getCause();
                        if (cause != null) {
                            if (cause instanceof QueryException) {
                                LOG.error(
                                    ((QueryException)cause).getFormatString());
                            } else {
                                LOG.error(cause.getMessage());
                            }
                        }
                        if (ex instanceof ContentInsertException) {
                            Content content = 
                                    ((ContentInsertException)ex).getContent();
                            DocumentURI failedUri = 
                                    pendingUris[id].remove(content);
                            failed++;
                            if (failedUri != null) {
                                LOG.warn("Failed document " + failedUri);
                            }
                        }
                    }
                }
                if (retry > 0) { 
                    LOG.debug("Retry successful");
                }
            } catch (Exception e) {
                boolean retryable = true;
                if (e instanceof QueryException) {
                    LOG.error("QueryException:" + ((QueryException)e).getFormatString());
                    retryable = ((QueryException)e).isRetryable();
                } else if (e instanceof RequestServerException) {
                    // log error and continue on RequestServerException
                    // not retryable so trying to connect to the replica
                    LOG.error("RequestServerException:" + e.getMessage());
                } else {
                    LOG.error("Exception:" + e.getMessage());
                }

                // necessary to roll back in certain scenarios.
                if (needCommit) {
                    rollback(id);
                }

                if (retryable && ++retry < maxRetries) {
                    // necessary to close the session too.
                    sessions[id].close();
                    try {
                        InternalUtilities.sleep(sleepTime);
                    } catch (Exception e2) {
                    }
                    sleepTime = sleepTime * 2;
                    if (sleepTime > maxSleepTime) 
                        sleepTime = maxSleepTime;
                    
                    // We need to switch host even for RetryableQueryException
                    // because it could be "sync replicating" (bug:45785)
                    sessions[id] = getSession(id, true);
                    continue;
                } else if (retryable) {
                    LOG.info("Exceeded max retry");
                }
                failed += batch.length;
                // remove the failed content from pendingUris
                for (Content fc : batch) {
                    DocumentURI failedUri = pendingUris[id].remove(fc);
                    LOG.warn("Failed document " + failedUri);
                }
                throw new IOException(e);
            } 
            break;
        }

        if (needCommit) {
            // move uris from pending to commit
            for (DocumentURI uri : pendingUris[id].values()) {
                commitUris[id].add(uri);
            }
        } else {
            succeeded += pendingUris[id].size();
        }
        pendingUris[id].clear();
    }
507520
private void sendRequest(Object request, ContextualFuture future) {
    if (open && connection.open) {
      long requestId = ++this.requestId;
      futures.put(requestId, future);
      connection.handleRequest(requestId, request);
    } else {
      future.context.executor().execute(() -> future.completeExceptionally(new ConnectException("connection closed")));
    }

    if (request instanceof ReferenceCounted) {
      ((ReferenceCounted<?>) request).release();
    }
  }
146511
@Override
  public void putFailure(K key, V value, StoreAccessException e) {
    try {
      loaderWriter.write(key, value);
    } catch (Exception e1) {
      throw ExceptionFactory.newCacheWritingException(e1, e);
    } finally {
      cleanup(key, e);
    }
  }
1036044
void handleRequest(ByteBuf buffer) {
    long requestId = buffer.readLong();

    try {
      Object request = readRequest(buffer);
      HandlerHolder handler = handlers.get(request.getClass());
      if (handler != null) {
        handler.context.executor().execute(() -> handleRequest(requestId, request, handler));
      } else {
        handleRequestFailure(requestId, new SerializationException("unknown message type: " + request.getClass()), this.context);
      }
    } catch (SerializationException e) {
      handleRequestFailure(requestId, e, this.context);
    } finally {
      buffer.release();
    }
  }
107505
public void requestFailed (final Exception cause)
    {
        EventQueue.invokeLater(new Runnable() {
            public void run () {
                _target.requestFailed(cause);
            }
        });
    }
1124581
private void fireConnectionError(SQLException e) {
    Code code = Code.UNKNOWN;
    if (e instanceof CloudSpannerSQLException) {
      code = ((CloudSpannerSQLException) e).getCode();
    }
    if (!isFatalState(code)) {
      return;
    }
    fireConnectionFatalError(e);
  }
437142
def handle_integrity_error(cls, exception):
        """"""

        m = re.match(cls.MYSQL_INSERT_ERROR_REGEX,
                     exception.statement)

        if not m:
            raise exception

        model = find_model_by_table_name(m.group('table'))

        if not model:
            raise exception

        m = re.match(cls.MYSQL_DUPLICATE_ENTRY_ERROR_REGEX,
                     exception.orig.args[1])

        if not m:
            raise exception

        entity = model.__name__
        eid = m.group('value')

        raise AlreadyExistsError(entity=entity, eid=eid)
499919
function onConnect(err) {
    if (err) {
      DEBUG("connect error", err)
      return cb(err)
    }

    DEBUG("connected")

    channel.sendMessage({
      type: "request",
      name: "connect",
      to:   "server",
      id:   "0",
      body: {
        key: config.key
      }
    })
  }
1297856
private Object handleFailOver(SQLException qe, Method method, Object[] args, Protocol protocol)
      throws Throwable {
    HostAddress failHostAddress = null;
    boolean failIsMaster = true;
    if (protocol != null) {
      failHostAddress = protocol.getHostAddress();
      failIsMaster = protocol.isMasterConnection();
    }

    HandleErrorResult handleErrorResult = listener.handleFailover(qe, method, args, protocol);
    if (handleErrorResult.mustThrowError) {
      listener
          .throwFailoverMessage(failHostAddress, failIsMaster, qe, handleErrorResult.isReconnected);
    }
    return handleErrorResult.resultObject;
  }
586270
def handle_query_error(msg, query, session, payload=None):
    """"""
    payload = payload or {}
    troubleshooting_link = config['TROUBLESHOOTING_LINK']
    query.error_message = msg
    query.status = QueryStatus.FAILED
    query.tmp_table_name = None
    session.commit()
    payload.update({
        'status': query.status,
        'error': msg,
    })
    if troubleshooting_link:
        payload['link'] = troubleshooting_link
    return payload
1105964
function (request, callback) {
            var connection = this._getConnection();
            var async = (request.async !== false);

            connection.open(request.method, request.url, async);

            for (var header in request.headers) {
                if (request.headers.hasOwnProperty(header)) {
                    connection.setRequestHeader(header, request.headers[header]);
                }
            }

            if (async) {
                // Timer for aborting the request after a timeout
                ariaCoreIO.setTimeout(request.id, request.timeout, {
                    fn : this.onAbort,
                    scope : this,
                    args : [request.id, connection]
                });
                this._handleReadyState(request.id, connection, callback);
            }

            // This might throw an error, propagate it and let the IO know that there was an exception
            connection.send(request.data || null);
            if (!async) {
                this._handleTransactionResponse(request.id, connection, callback);
            }
        }
1110454
protected void putConnectionBackInPartition(ConnectionHandle connectionHandle) throws SQLException {

		if (this.cachedPoolStrategy && ((CachedConnectionStrategy)this.connectionStrategy).tlConnections.dumbGet().getValue()){
			connectionHandle.logicallyClosed.set(true);
			((CachedConnectionStrategy)this.connectionStrategy).tlConnections.set(new AbstractMap.SimpleEntry<ConnectionHandle, Boolean>(connectionHandle, false));
		} else {
			BlockingQueue<ConnectionHandle> queue = connectionHandle.getOriginatingPartition().getFreeConnections();
				if (!queue.offer(connectionHandle)){ // this shouldn't fail
					connectionHandle.internalClose();
				}
		}


	}
1030451
public void noteRequestCompletion(ServerStats stats, Object response, Throwable e, long responseTime, RetryHandler errorHandler) {
    	if (stats == null) {
    		return;
    	}
        try {
            recordStats(stats, responseTime);
            RetryHandler callErrorHandler = errorHandler == null ? getRetryHandler() : errorHandler;
            if (callErrorHandler != null && response != null) {
                stats.clearSuccessiveConnectionFailureCount();
            } else if (callErrorHandler != null && e != null) {
                if (callErrorHandler.isCircuitTrippingException(e)) {
                    stats.incrementSuccessiveConnectionFailureCount();                    
                    stats.addToFailureCount();
                } else {
                    stats.clearSuccessiveConnectionFailureCount();
                }
            }
        } catch (Exception ex) {
            logger.error("Error noting stats for client {}", clientName, ex);
        }            
    }
487908
function(query, data, successCallback, failureCallback) {
			if(typeof successCallback != "function") successCallback = function(){};
			if(typeof failureCallback != "function") failureCallback = function(){ return _throw(new jSQL_Error("0054")); };

			var i, l, remaining;

			if(!Array.isArray(data[0])) data = [data];
			remaining = data.length;
			var innerSuccessCallback = function(tx, rs) {
				var i, l, output = [];
				remaining = remaining - 1;
				if (!remaining) {
					for (i = 0, l = rs.rows.length; i < l; i = i + 1){
						var j = rs.rows.item(i).json;
						//j = JSON.parse(j);
						output.push(j);
					}
					successCallback(output);
				}
			};
			self.db.transaction(function (tx) {
				for (i = 0, l = data.length; i < l; i = i + 1) {
					tx.executeSql(query, data[i], innerSuccessCallback, failureCallback);
				}
			});
		}
326075
def insertMigrationRequest(self, request):
        
        conn = self.dbi.connection()
        # check if already queued.
        #If the migration_input is the same, but the src url is different,
        #We will consider it as a submitted request. YG 05-18-2012
        try:
            alreadyqueued = self.mgrlist.execute(conn,
                                                 migration_input=request["migration_input"])
            is_already_queued = len(alreadyqueued) > 0
            # close connection before returning json object
            if is_already_queued and conn:
                conn.close()
            #if the queued is not failed, then we don't need to do it again.
            #add a new migration_status=9 (terminal failure)
	    if is_already_queued and alreadyqueued[0]['migration_status'] == 2:
                return {"migration_report" : "REQUEST ALREADY QUEUED. Migration is finished",
                        "migration_details" : alreadyqueued[0] }
            elif is_already_queued and alreadyqueued[0]['migration_status'] != 9:
                return {"migration_report" : "REQUEST ALREADY QUEUED. Migration in progress",
                        "migration_details" : alreadyqueued[0] }
            elif is_already_queued and alreadyqueued[0]['migration_status'] == 9:
                return {"migration_report" : "REQUEST ALREADY QUEUED. Migration terminally failed. ",
                        "migration_details" : alreadyqueued[0] }
            else:
                # not already queued
                #Determine if its a dataset or block migration
                #The prepare list calls will check if the requested blocks/dataset already in destination.
                if request["migration_input"].find("#") != -1:
                    ordered_list = self.prepareBlockMigrationList(conn, request)
                else:
                    ordered_list = self.prepareDatasetMigrationList(conn, request)
            # now we have the blocks that need to be queued (ordered)
        except Exception as ex:
            if conn: conn.close()
            raise

        tran = conn.begin()
        try:
            # insert the request
            #request.update(migration_status=0)
            request['migration_request_id'] = self.sm.increment(conn, "SEQ_MR", tran)
            self.mgrin.execute(conn, request, tran)
            # INSERT the ordered_list
            totalQueued = 0
	    k = ordered_list.keys()
	    k.sort()
	    k.reverse()	
	    self.logger.debug("****************** ordered_list keys **********")
            self.logger.debug(k)
            #for iter in reversed(range(len(ordered_list))):
	    for iter in k:	
		self.logger.debug("length for Key: %s" %iter)
	        self.logger.debug(len(ordered_list[iter]))	
                if len(ordered_list[iter]) > 0:
                    daoinput = [{
                        "migration_block_id" :
                            self.sm.increment(conn, "SEQ_MB", tran),
                        "migration_request_id" :
                            request["migration_request_id"],
                        "migration_block_name" : blk,
                        "migration_order" : iter,
                        "migration_status" : 0,
                        "creation_date" : request['creation_date'],
                        "last_modification_date" : request['last_modification_date'],
                        "create_by" : request['create_by'],
                        "last_modified_by" : request['last_modified_by']
                        }
                             for blk in ordered_list[iter]]
                    self.mgrblkin.execute(conn, daoinput, tran)
                    totalQueued += len(ordered_list[iter])
            # all good ?, commit the transaction
            tran.commit()
            if conn: conn.close()
            # return things like (X blocks queued for migration)
            return {
                "migration_report" : "REQUEST QUEUED with total %d blocks to be migrated" %totalQueued,
                "migration_details" : request }
        except SQLAlchemyIntegrityError as ex:
	    e = "DBSMigration:  ENQUEUEING_FAILED1 from SQLAichemy Integrity Error. Reason may be (%s)" %(ex.statement + "; " + str(ex.params) + "; " + str(ex.orig))
	    self.logger.debug(e)		
	    import traceback
            tk = traceback.format_exc() 		
	    self.logger.debug(tk) 	
            tran.rollback()
            if conn: conn.close()
            if (str(ex).find("unique constraint") != -1 or
                str(ex).lower().find("duplicate") != -1):
                #FIXME: Need to check which unique key. YG 2/11/13
                #The unique constraints are: MIGRATION_REQUESTS(MIGRATION_INPUT)
                #MIGRATION_BLOCKS(MIGRATION_BLOCK_NAME, MIGRATION_REQUEST_ID)
                return {
                    "migration_report" : "REQUEST ALREADY QUEUED",
                    "migration_details" : request }
            else:
                if conn: conn.close()
		self.logger.error(tk)
                m = "DBSMigration:  ENQUEUEING_FAILED1."
                dbsExceptionHandler('dbsException-invalid-input2', m, self.logger.exception, e)
	except HTTPError as he:
	    raise he	
        except Exception as ex:
	    import traceback
            self.logger.error(traceback.format_exc())	
            if tran: tran.rollback()
            if conn: conn.close()
            m = "DBSMigration:  ENQUEUEING_FAILED."
            e = "DBSMigration:  ENQUEUEING_FAILED. General exception caught: Reason may be (%s)" %str(ex)
            dbsExceptionHandler('dbsException-invalid-input2', m, self.logger.exception, e)
        finally:
            if conn: conn.close()