001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.client;
021
022import org.apache.hadoop.hbase.ServerName;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026import java.util.ArrayList;
027import java.util.List;
028
029class BatchErrors {
030  private static final Logger LOG = LoggerFactory.getLogger(BatchErrors.class);
031  final List<Throwable> throwables = new ArrayList<>();
032  final List<Row> actions = new ArrayList<>();
033  final List<String> addresses = new ArrayList<>();
034
035  public synchronized void add(Throwable ex, Row row, ServerName serverName) {
036    if (row == null){
037      throw new IllegalArgumentException("row cannot be null. location=" + serverName);
038    }
039
040    throwables.add(ex);
041    actions.add(row);
042    addresses.add(serverName != null ? serverName.toString() : "null");
043  }
044
045  public boolean hasErrors() {
046    return !throwables.isEmpty();
047  }
048
049  synchronized RetriesExhaustedWithDetailsException makeException(boolean logDetails) {
050    if (logDetails) {
051      LOG.error("Exception occurred! Exception details: " + throwables + ";\nActions: "
052              + actions);
053    }
054    return new RetriesExhaustedWithDetailsException(new ArrayList<>(throwables),
055            new ArrayList<>(actions), new ArrayList<>(addresses));
056  }
057
058  public synchronized void clear() {
059    throwables.clear();
060    actions.clear();
061    addresses.clear();
062  }
063
064  public synchronized void merge(BatchErrors other) {
065    throwables.addAll(other.throwables);
066    actions.addAll(other.actions);
067    addresses.addAll(other.addresses);
068  }
069}