PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/hadoop-hdfs-project/hadoop-hdfs/src/contrib/bkjournal/src/main/java/org/apache/hadoop/contrib/bkjournal/EditLogLedgerMetadata.java

https://gitlab.com/xiaoliuliu2050/hadoop
Java | 217 lines | 169 code | 27 blank | 21 comment | 20 complexity | ea90e0c263ec1d12276c1811492b5f60 MD5 | raw file
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.contrib.bkjournal;
  19. import java.io.IOException;
  20. import java.util.Comparator;
  21. import org.apache.zookeeper.ZooKeeper;
  22. import org.apache.zookeeper.CreateMode;
  23. import org.apache.zookeeper.ZooDefs.Ids;
  24. import org.apache.zookeeper.KeeperException;
  25. import org.apache.hadoop.hdfs.protocol.HdfsConstants;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.apache.hadoop.contrib.bkjournal.BKJournalProtos.EditLogLedgerProto;
  29. import com.google.protobuf.TextFormat;
  30. import static com.google.common.base.Charsets.UTF_8;
  31. /**
  32. * Utility class for storing the metadata associated
  33. * with a single edit log segment, stored in a single ledger
  34. */
  35. public class EditLogLedgerMetadata {
  36. static final Log LOG = LogFactory.getLog(EditLogLedgerMetadata.class);
  37. private String zkPath;
  38. private final int dataLayoutVersion;
  39. private final long ledgerId;
  40. private final long firstTxId;
  41. private long lastTxId;
  42. private boolean inprogress;
  43. public static final Comparator COMPARATOR
  44. = new Comparator<EditLogLedgerMetadata>() {
  45. public int compare(EditLogLedgerMetadata o1,
  46. EditLogLedgerMetadata o2) {
  47. if (o1.firstTxId < o2.firstTxId) {
  48. return -1;
  49. } else if (o1.firstTxId == o2.firstTxId) {
  50. return 0;
  51. } else {
  52. return 1;
  53. }
  54. }
  55. };
  56. EditLogLedgerMetadata(String zkPath, int dataLayoutVersion,
  57. long ledgerId, long firstTxId) {
  58. this.zkPath = zkPath;
  59. this.dataLayoutVersion = dataLayoutVersion;
  60. this.ledgerId = ledgerId;
  61. this.firstTxId = firstTxId;
  62. this.lastTxId = HdfsConstants.INVALID_TXID;
  63. this.inprogress = true;
  64. }
  65. EditLogLedgerMetadata(String zkPath, int dataLayoutVersion,
  66. long ledgerId, long firstTxId,
  67. long lastTxId) {
  68. this.zkPath = zkPath;
  69. this.dataLayoutVersion = dataLayoutVersion;
  70. this.ledgerId = ledgerId;
  71. this.firstTxId = firstTxId;
  72. this.lastTxId = lastTxId;
  73. this.inprogress = false;
  74. }
  75. String getZkPath() {
  76. return zkPath;
  77. }
  78. long getFirstTxId() {
  79. return firstTxId;
  80. }
  81. long getLastTxId() {
  82. return lastTxId;
  83. }
  84. long getLedgerId() {
  85. return ledgerId;
  86. }
  87. boolean isInProgress() {
  88. return this.inprogress;
  89. }
  90. int getDataLayoutVersion() {
  91. return this.dataLayoutVersion;
  92. }
  93. void finalizeLedger(long newLastTxId) {
  94. assert this.lastTxId == HdfsConstants.INVALID_TXID;
  95. this.lastTxId = newLastTxId;
  96. this.inprogress = false;
  97. }
  98. static EditLogLedgerMetadata read(ZooKeeper zkc, String path)
  99. throws IOException, KeeperException.NoNodeException {
  100. try {
  101. byte[] data = zkc.getData(path, false, null);
  102. EditLogLedgerProto.Builder builder = EditLogLedgerProto.newBuilder();
  103. if (LOG.isDebugEnabled()) {
  104. LOG.debug("Reading " + path + " data: " + new String(data, UTF_8));
  105. }
  106. TextFormat.merge(new String(data, UTF_8), builder);
  107. if (!builder.isInitialized()) {
  108. throw new IOException("Invalid/Incomplete data in znode");
  109. }
  110. EditLogLedgerProto ledger = builder.build();
  111. int dataLayoutVersion = ledger.getDataLayoutVersion();
  112. long ledgerId = ledger.getLedgerId();
  113. long firstTxId = ledger.getFirstTxId();
  114. if (ledger.hasLastTxId()) {
  115. long lastTxId = ledger.getLastTxId();
  116. return new EditLogLedgerMetadata(path, dataLayoutVersion,
  117. ledgerId, firstTxId, lastTxId);
  118. } else {
  119. return new EditLogLedgerMetadata(path, dataLayoutVersion,
  120. ledgerId, firstTxId);
  121. }
  122. } catch(KeeperException.NoNodeException nne) {
  123. throw nne;
  124. } catch(KeeperException ke) {
  125. throw new IOException("Error reading from zookeeper", ke);
  126. } catch (InterruptedException ie) {
  127. throw new IOException("Interrupted reading from zookeeper", ie);
  128. }
  129. }
  130. void write(ZooKeeper zkc, String path)
  131. throws IOException, KeeperException.NodeExistsException {
  132. this.zkPath = path;
  133. EditLogLedgerProto.Builder builder = EditLogLedgerProto.newBuilder();
  134. builder.setDataLayoutVersion(dataLayoutVersion)
  135. .setLedgerId(ledgerId).setFirstTxId(firstTxId);
  136. if (!inprogress) {
  137. builder.setLastTxId(lastTxId);
  138. }
  139. try {
  140. zkc.create(path, TextFormat.printToString(builder.build()).getBytes(UTF_8),
  141. Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  142. } catch (KeeperException.NodeExistsException nee) {
  143. throw nee;
  144. } catch (KeeperException e) {
  145. throw new IOException("Error creating ledger znode", e);
  146. } catch (InterruptedException ie) {
  147. throw new IOException("Interrupted creating ledger znode", ie);
  148. }
  149. }
  150. boolean verify(ZooKeeper zkc, String path) {
  151. try {
  152. EditLogLedgerMetadata other = read(zkc, path);
  153. if (LOG.isTraceEnabled()) {
  154. LOG.trace("Verifying " + this.toString()
  155. + " against " + other);
  156. }
  157. return other.equals(this);
  158. } catch (KeeperException e) {
  159. LOG.error("Couldn't verify data in " + path, e);
  160. return false;
  161. } catch (IOException ie) {
  162. LOG.error("Couldn't verify data in " + path, ie);
  163. return false;
  164. }
  165. }
  166. public boolean equals(Object o) {
  167. if (!(o instanceof EditLogLedgerMetadata)) {
  168. return false;
  169. }
  170. EditLogLedgerMetadata ol = (EditLogLedgerMetadata)o;
  171. return ledgerId == ol.ledgerId
  172. && dataLayoutVersion == ol.dataLayoutVersion
  173. && firstTxId == ol.firstTxId
  174. && lastTxId == ol.lastTxId;
  175. }
  176. public int hashCode() {
  177. int hash = 1;
  178. hash = hash * 31 + (int) ledgerId;
  179. hash = hash * 31 + (int) firstTxId;
  180. hash = hash * 31 + (int) lastTxId;
  181. hash = hash * 31 + dataLayoutVersion;
  182. return hash;
  183. }
  184. public String toString() {
  185. return "[LedgerId:"+ledgerId +
  186. ", firstTxId:" + firstTxId +
  187. ", lastTxId:" + lastTxId +
  188. ", dataLayoutVersion:" + dataLayoutVersion + "]";
  189. }
  190. }