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