summaryrefslogtreecommitdiff
path: root/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/helpers/DeletedContainerBlocksSummary.java
blob: ade162a26372acd392c88df53fbdd4fcd9a38a0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with this
 * work for additional information regarding copyright ownership.  The ASF
 * licenses this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.apache.hadoop.ozone.container.common.helpers;

import com.google.common.collect.Maps;
import org.apache.hadoop.hdds.protocol.proto
    .StorageContainerDatanodeProtocolProtos.DeletedBlocksTransaction;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * A helper class to wrap the info about under deletion container blocks.
 */
public final class DeletedContainerBlocksSummary {

  private final List<DeletedBlocksTransaction> blocks;
  // key : txID
  // value : times of this tx has been processed
  private final Map<Long, Integer> txSummary;
  // key : container name
  // value : the number of blocks need to be deleted in this container
  // if the message contains multiple entries for same block,
  // blocks will be merged
  private final Map<String, Integer> blockSummary;
  // total number of blocks in this message
  private int numOfBlocks;

  private DeletedContainerBlocksSummary(List<DeletedBlocksTransaction> blocks) {
    this.blocks = blocks;
    txSummary = Maps.newHashMap();
    blockSummary = Maps.newHashMap();
    blocks.forEach(entry -> {
      txSummary.put(entry.getTxID(), entry.getCount());
      if (blockSummary.containsKey(entry.getContainerName())) {
        blockSummary.put(entry.getContainerName(),
            blockSummary.get(entry.getContainerName())
                + entry.getBlockIDCount());
      } else {
        blockSummary.put(entry.getContainerName(), entry.getBlockIDCount());
      }
      numOfBlocks += entry.getBlockIDCount();
    });
  }

  public static DeletedContainerBlocksSummary getFrom(
      List<DeletedBlocksTransaction> blocks) {
    return new DeletedContainerBlocksSummary(blocks);
  }

  public int getNumOfBlocks() {
    return numOfBlocks;
  }

  public int getNumOfContainers() {
    return blockSummary.size();
  }

  public String getTXIDs() {
    return String.join(",", txSummary.keySet()
        .stream().map(String::valueOf).collect(Collectors.toList()));
  }

  public String getTxIDSummary() {
    List<String> txSummaryEntry = txSummary.entrySet().stream()
        .map(entry -> entry.getKey() + "(" + entry.getValue() + ")")
        .collect(Collectors.toList());
    return "[" + String.join(",", txSummaryEntry) + "]";
  }

  @Override public String toString() {
    StringBuffer sb = new StringBuffer();
    for (DeletedBlocksTransaction blks : blocks) {
      sb.append(" ")
          .append("TXID=")
          .append(blks.getTxID())
          .append(", ")
          .append("TimesProceed=")
          .append(blks.getCount())
          .append(", ")
          .append(blks.getContainerName())
          .append(" : [")
          .append(String.join(",", blks.getBlockIDList())).append("]")
          .append("\n");
    }
    return sb.toString();
  }
}