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.hdfs.server.namenode; 019 020import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_ADD; 021import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_ADD_BLOCK; 022import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_ADD_CACHE_DIRECTIVE; 023import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_ADD_CACHE_POOL; 024import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_ALLOCATE_BLOCK_ID; 025import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_ALLOW_SNAPSHOT; 026import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_CANCEL_DELEGATION_TOKEN; 027import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_CLEAR_NS_QUOTA; 028import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_CLOSE; 029import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_CONCAT_DELETE; 030import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_CREATE_SNAPSHOT; 031import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_DELETE; 032import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_DELETE_SNAPSHOT; 033import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_DISALLOW_SNAPSHOT; 034import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_END_LOG_SEGMENT; 035import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_GET_DELEGATION_TOKEN; 036import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_INVALID; 037import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_MKDIR; 038import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_MODIFY_CACHE_DIRECTIVE; 039import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_MODIFY_CACHE_POOL; 040import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_REASSIGN_LEASE; 041import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_REMOVE_CACHE_DIRECTIVE; 042import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_REMOVE_CACHE_POOL; 043import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_REMOVE_XATTR; 044import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_RENAME; 045import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_RENAME_OLD; 046import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_RENAME_SNAPSHOT; 047import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_RENEW_DELEGATION_TOKEN; 048import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_SET_ACL; 049import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_ROLLING_UPGRADE_FINALIZE; 050import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_ROLLING_UPGRADE_START; 051import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_SET_GENSTAMP_V1; 052import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_SET_GENSTAMP_V2; 053import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_SET_NS_QUOTA; 054import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_SET_OWNER; 055import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_SET_PERMISSIONS; 056import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_SET_QUOTA; 057import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_SET_REPLICATION; 058import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_SET_XATTR; 059import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_START_LOG_SEGMENT; 060import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_SYMLINK; 061import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_TIMES; 062import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_UPDATE_BLOCKS; 063import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_UPDATE_MASTER_KEY; 064import static org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes.OP_SET_STORAGE_POLICY; 065 066import java.io.DataInput; 067import java.io.DataInputStream; 068import java.io.DataOutput; 069import java.io.DataOutputStream; 070import java.io.EOFException; 071import java.io.IOException; 072import java.util.ArrayList; 073import java.util.Arrays; 074import java.util.EnumMap; 075import java.util.List; 076import java.util.zip.CheckedInputStream; 077import java.util.zip.Checksum; 078 079import org.apache.commons.codec.DecoderException; 080import org.apache.commons.codec.binary.Hex; 081import org.apache.hadoop.classification.InterfaceAudience; 082import org.apache.hadoop.classification.InterfaceStability; 083import org.apache.hadoop.fs.ChecksumException; 084import org.apache.hadoop.fs.Options.Rename; 085import org.apache.hadoop.fs.XAttr; 086import org.apache.hadoop.fs.XAttrCodec; 087import org.apache.hadoop.fs.permission.AclEntry; 088import org.apache.hadoop.fs.permission.AclEntryScope; 089import org.apache.hadoop.fs.permission.AclEntryType; 090import org.apache.hadoop.fs.permission.FsAction; 091import org.apache.hadoop.fs.permission.FsPermission; 092import org.apache.hadoop.fs.permission.PermissionStatus; 093import org.apache.hadoop.hdfs.DFSConfigKeys; 094import org.apache.hadoop.hdfs.DeprecatedUTF8; 095import org.apache.hadoop.hdfs.protocol.Block; 096import org.apache.hadoop.hdfs.protocol.CacheDirectiveInfo; 097import org.apache.hadoop.hdfs.protocol.CachePoolInfo; 098import org.apache.hadoop.hdfs.protocol.ClientProtocol; 099import org.apache.hadoop.hdfs.protocol.HdfsConstants; 100import org.apache.hadoop.hdfs.protocol.LayoutVersion; 101import org.apache.hadoop.hdfs.protocol.LayoutVersion.Feature; 102import org.apache.hadoop.hdfs.protocol.proto.AclProtos.AclEditLogProto; 103import org.apache.hadoop.hdfs.protocol.proto.XAttrProtos.XAttrEditLogProto; 104import org.apache.hadoop.hdfs.protocolPB.PBHelper; 105import org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenIdentifier; 106import org.apache.hadoop.hdfs.server.blockmanagement.BlockStoragePolicySuite; 107import org.apache.hadoop.hdfs.util.XMLUtils; 108import org.apache.hadoop.hdfs.util.XMLUtils.InvalidXmlException; 109import org.apache.hadoop.hdfs.util.XMLUtils.Stanza; 110import org.apache.hadoop.io.ArrayWritable; 111import org.apache.hadoop.io.BytesWritable; 112import org.apache.hadoop.io.DataOutputBuffer; 113import org.apache.hadoop.io.IOUtils; 114import org.apache.hadoop.io.Text; 115import org.apache.hadoop.io.Writable; 116import org.apache.hadoop.io.WritableFactories; 117import org.apache.hadoop.io.WritableFactory; 118import org.apache.hadoop.ipc.ClientId; 119import org.apache.hadoop.ipc.RpcConstants; 120import org.apache.hadoop.security.token.delegation.DelegationKey; 121import org.apache.hadoop.util.DataChecksum; 122import org.xml.sax.ContentHandler; 123import org.xml.sax.SAXException; 124import org.xml.sax.helpers.AttributesImpl; 125 126import com.google.common.annotations.VisibleForTesting; 127import com.google.common.base.Joiner; 128import com.google.common.base.Preconditions; 129import com.google.common.collect.ImmutableMap; 130import com.google.common.collect.Lists; 131 132/** 133 * Helper classes for reading the ops from an InputStream. 134 * All ops derive from FSEditLogOp and are only 135 * instantiated from Reader#readOp() 136 */ 137@InterfaceAudience.Private 138@InterfaceStability.Unstable 139public abstract class FSEditLogOp { 140 public final FSEditLogOpCodes opCode; 141 long txid; 142 byte[] rpcClientId; 143 int rpcCallId; 144 145 final void reset() { 146 txid = HdfsConstants.INVALID_TXID; 147 rpcClientId = RpcConstants.DUMMY_CLIENT_ID; 148 rpcCallId = RpcConstants.INVALID_CALL_ID; 149 resetSubFields(); 150 } 151 152 abstract void resetSubFields(); 153 154 final public static class OpInstanceCache { 155 private final EnumMap<FSEditLogOpCodes, FSEditLogOp> inst = 156 new EnumMap<FSEditLogOpCodes, FSEditLogOp>(FSEditLogOpCodes.class); 157 158 public OpInstanceCache() { 159 inst.put(OP_ADD, new AddOp()); 160 inst.put(OP_CLOSE, new CloseOp()); 161 inst.put(OP_SET_REPLICATION, new SetReplicationOp()); 162 inst.put(OP_CONCAT_DELETE, new ConcatDeleteOp()); 163 inst.put(OP_RENAME_OLD, new RenameOldOp()); 164 inst.put(OP_DELETE, new DeleteOp()); 165 inst.put(OP_MKDIR, new MkdirOp()); 166 inst.put(OP_SET_GENSTAMP_V1, new SetGenstampV1Op()); 167 inst.put(OP_SET_PERMISSIONS, new SetPermissionsOp()); 168 inst.put(OP_SET_OWNER, new SetOwnerOp()); 169 inst.put(OP_SET_NS_QUOTA, new SetNSQuotaOp()); 170 inst.put(OP_CLEAR_NS_QUOTA, new ClearNSQuotaOp()); 171 inst.put(OP_SET_QUOTA, new SetQuotaOp()); 172 inst.put(OP_TIMES, new TimesOp()); 173 inst.put(OP_SYMLINK, new SymlinkOp()); 174 inst.put(OP_RENAME, new RenameOp()); 175 inst.put(OP_REASSIGN_LEASE, new ReassignLeaseOp()); 176 inst.put(OP_GET_DELEGATION_TOKEN, new GetDelegationTokenOp()); 177 inst.put(OP_RENEW_DELEGATION_TOKEN, new RenewDelegationTokenOp()); 178 inst.put(OP_CANCEL_DELEGATION_TOKEN, new CancelDelegationTokenOp()); 179 inst.put(OP_UPDATE_MASTER_KEY, new UpdateMasterKeyOp()); 180 inst.put(OP_START_LOG_SEGMENT, new LogSegmentOp(OP_START_LOG_SEGMENT)); 181 inst.put(OP_END_LOG_SEGMENT, new LogSegmentOp(OP_END_LOG_SEGMENT)); 182 inst.put(OP_UPDATE_BLOCKS, new UpdateBlocksOp()); 183 184 inst.put(OP_ALLOW_SNAPSHOT, new AllowSnapshotOp()); 185 inst.put(OP_DISALLOW_SNAPSHOT, new DisallowSnapshotOp()); 186 inst.put(OP_CREATE_SNAPSHOT, new CreateSnapshotOp()); 187 inst.put(OP_DELETE_SNAPSHOT, new DeleteSnapshotOp()); 188 inst.put(OP_RENAME_SNAPSHOT, new RenameSnapshotOp()); 189 inst.put(OP_SET_GENSTAMP_V2, new SetGenstampV2Op()); 190 inst.put(OP_ALLOCATE_BLOCK_ID, new AllocateBlockIdOp()); 191 inst.put(OP_ADD_BLOCK, new AddBlockOp()); 192 inst.put(OP_ADD_CACHE_DIRECTIVE, 193 new AddCacheDirectiveInfoOp()); 194 inst.put(OP_MODIFY_CACHE_DIRECTIVE, 195 new ModifyCacheDirectiveInfoOp()); 196 inst.put(OP_REMOVE_CACHE_DIRECTIVE, 197 new RemoveCacheDirectiveInfoOp()); 198 inst.put(OP_ADD_CACHE_POOL, new AddCachePoolOp()); 199 inst.put(OP_MODIFY_CACHE_POOL, new ModifyCachePoolOp()); 200 inst.put(OP_REMOVE_CACHE_POOL, new RemoveCachePoolOp()); 201 202 inst.put(OP_SET_ACL, new SetAclOp()); 203 inst.put(OP_ROLLING_UPGRADE_START, new RollingUpgradeOp( 204 OP_ROLLING_UPGRADE_START, "start")); 205 inst.put(OP_ROLLING_UPGRADE_FINALIZE, new RollingUpgradeOp( 206 OP_ROLLING_UPGRADE_FINALIZE, "finalize")); 207 inst.put(OP_SET_XATTR, new SetXAttrOp()); 208 inst.put(OP_REMOVE_XATTR, new RemoveXAttrOp()); 209 inst.put(OP_SET_STORAGE_POLICY, new SetStoragePolicyOp()); 210 } 211 212 public FSEditLogOp get(FSEditLogOpCodes opcode) { 213 return inst.get(opcode); 214 } 215 } 216 217 private static ImmutableMap<String, FsAction> fsActionMap() { 218 ImmutableMap.Builder<String, FsAction> b = ImmutableMap.builder(); 219 for (FsAction v : FsAction.values()) 220 b.put(v.SYMBOL, v); 221 return b.build(); 222 } 223 224 private static final ImmutableMap<String, FsAction> FSACTION_SYMBOL_MAP 225 = fsActionMap(); 226 227 /** 228 * Constructor for an EditLog Op. EditLog ops cannot be constructed 229 * directly, but only through Reader#readOp. 230 */ 231 @VisibleForTesting 232 protected FSEditLogOp(FSEditLogOpCodes opCode) { 233 this.opCode = opCode; 234 reset(); 235 } 236 237 public long getTransactionId() { 238 Preconditions.checkState(txid != HdfsConstants.INVALID_TXID); 239 return txid; 240 } 241 242 public String getTransactionIdStr() { 243 return (txid == HdfsConstants.INVALID_TXID) ? "(none)" : "" + txid; 244 } 245 246 public boolean hasTransactionId() { 247 return (txid != HdfsConstants.INVALID_TXID); 248 } 249 250 public void setTransactionId(long txid) { 251 this.txid = txid; 252 } 253 254 public boolean hasRpcIds() { 255 return rpcClientId != RpcConstants.DUMMY_CLIENT_ID 256 && rpcCallId != RpcConstants.INVALID_CALL_ID; 257 } 258 259 /** this has to be called after calling {@link #hasRpcIds()} */ 260 public byte[] getClientId() { 261 Preconditions.checkState(rpcClientId != RpcConstants.DUMMY_CLIENT_ID); 262 return rpcClientId; 263 } 264 265 public void setRpcClientId(byte[] clientId) { 266 this.rpcClientId = clientId; 267 } 268 269 /** this has to be called after calling {@link #hasRpcIds()} */ 270 public int getCallId() { 271 Preconditions.checkState(rpcCallId != RpcConstants.INVALID_CALL_ID); 272 return rpcCallId; 273 } 274 275 public void setRpcCallId(int callId) { 276 this.rpcCallId = callId; 277 } 278 279 abstract void readFields(DataInputStream in, int logVersion) 280 throws IOException; 281 282 public abstract void writeFields(DataOutputStream out) 283 throws IOException; 284 285 static interface BlockListUpdatingOp { 286 Block[] getBlocks(); 287 String getPath(); 288 boolean shouldCompleteLastBlock(); 289 } 290 291 private static void writeRpcIds(final byte[] clientId, final int callId, 292 DataOutputStream out) throws IOException { 293 FSImageSerialization.writeBytes(clientId, out); 294 FSImageSerialization.writeInt(callId, out); 295 } 296 297 void readRpcIds(DataInputStream in, int logVersion) 298 throws IOException { 299 if (NameNodeLayoutVersion.supports( 300 LayoutVersion.Feature.EDITLOG_SUPPORT_RETRYCACHE, logVersion)) { 301 this.rpcClientId = FSImageSerialization.readBytes(in); 302 this.rpcCallId = FSImageSerialization.readInt(in); 303 } 304 } 305 306 void readRpcIdsFromXml(Stanza st) { 307 this.rpcClientId = st.hasChildren("RPC_CLIENTID") ? 308 ClientId.toBytes(st.getValue("RPC_CLIENTID")) 309 : RpcConstants.DUMMY_CLIENT_ID; 310 this.rpcCallId = st.hasChildren("RPC_CALLID") ? 311 Integer.parseInt(st.getValue("RPC_CALLID")) 312 : RpcConstants.INVALID_CALL_ID; 313 } 314 315 private static void appendRpcIdsToString(final StringBuilder builder, 316 final byte[] clientId, final int callId) { 317 builder.append(", RpcClientId="); 318 builder.append(ClientId.toString(clientId)); 319 builder.append(", RpcCallId="); 320 builder.append(callId); 321 } 322 323 private static void appendRpcIdsToXml(ContentHandler contentHandler, 324 final byte[] clientId, final int callId) throws SAXException { 325 XMLUtils.addSaxString(contentHandler, "RPC_CLIENTID", 326 ClientId.toString(clientId)); 327 XMLUtils.addSaxString(contentHandler, "RPC_CALLID", 328 Integer.toString(callId)); 329 } 330 331 private static final class AclEditLogUtil { 332 private static final int ACL_EDITLOG_ENTRY_HAS_NAME_OFFSET = 6; 333 private static final int ACL_EDITLOG_ENTRY_TYPE_OFFSET = 3; 334 private static final int ACL_EDITLOG_ENTRY_SCOPE_OFFSET = 5; 335 private static final int ACL_EDITLOG_PERM_MASK = 7; 336 private static final int ACL_EDITLOG_ENTRY_TYPE_MASK = 3; 337 private static final int ACL_EDITLOG_ENTRY_SCOPE_MASK = 1; 338 339 private static final FsAction[] FSACTION_VALUES = FsAction.values(); 340 private static final AclEntryScope[] ACL_ENTRY_SCOPE_VALUES = AclEntryScope 341 .values(); 342 private static final AclEntryType[] ACL_ENTRY_TYPE_VALUES = AclEntryType 343 .values(); 344 345 private static List<AclEntry> read(DataInputStream in, int logVersion) 346 throws IOException { 347 if (!NameNodeLayoutVersion.supports(Feature.EXTENDED_ACL, logVersion)) { 348 return null; 349 } 350 351 int size = in.readInt(); 352 if (size == 0) { 353 return null; 354 } 355 356 List<AclEntry> aclEntries = Lists.newArrayListWithCapacity(size); 357 for (int i = 0; i < size; ++i) { 358 int v = in.read(); 359 int p = v & ACL_EDITLOG_PERM_MASK; 360 int t = (v >> ACL_EDITLOG_ENTRY_TYPE_OFFSET) 361 & ACL_EDITLOG_ENTRY_TYPE_MASK; 362 int s = (v >> ACL_EDITLOG_ENTRY_SCOPE_OFFSET) 363 & ACL_EDITLOG_ENTRY_SCOPE_MASK; 364 boolean hasName = ((v >> ACL_EDITLOG_ENTRY_HAS_NAME_OFFSET) & 1) == 1; 365 String name = hasName ? FSImageSerialization.readString(in) : null; 366 aclEntries.add(new AclEntry.Builder().setName(name) 367 .setPermission(FSACTION_VALUES[p]) 368 .setScope(ACL_ENTRY_SCOPE_VALUES[s]) 369 .setType(ACL_ENTRY_TYPE_VALUES[t]).build()); 370 } 371 372 return aclEntries; 373 } 374 375 private static void write(List<AclEntry> aclEntries, DataOutputStream out) 376 throws IOException { 377 if (aclEntries == null) { 378 out.writeInt(0); 379 return; 380 } 381 382 out.writeInt(aclEntries.size()); 383 for (AclEntry e : aclEntries) { 384 boolean hasName = e.getName() != null; 385 int v = (e.getScope().ordinal() << ACL_EDITLOG_ENTRY_SCOPE_OFFSET) 386 | (e.getType().ordinal() << ACL_EDITLOG_ENTRY_TYPE_OFFSET) 387 | e.getPermission().ordinal(); 388 389 if (hasName) { 390 v |= 1 << ACL_EDITLOG_ENTRY_HAS_NAME_OFFSET; 391 } 392 out.write(v); 393 if (hasName) { 394 FSImageSerialization.writeString(e.getName(), out); 395 } 396 } 397 } 398 } 399 400 private static List<XAttr> readXAttrsFromEditLog(DataInputStream in, 401 int logVersion) throws IOException { 402 if (!NameNodeLayoutVersion.supports(NameNodeLayoutVersion.Feature.XATTRS, 403 logVersion)) { 404 return null; 405 } 406 XAttrEditLogProto proto = XAttrEditLogProto.parseDelimitedFrom(in); 407 return PBHelper.convertXAttrs(proto.getXAttrsList()); 408 } 409 410 @SuppressWarnings("unchecked") 411 static abstract class AddCloseOp extends FSEditLogOp implements BlockListUpdatingOp { 412 int length; 413 long inodeId; 414 String path; 415 short replication; 416 long mtime; 417 long atime; 418 long blockSize; 419 Block[] blocks; 420 PermissionStatus permissions; 421 List<AclEntry> aclEntries; 422 List<XAttr> xAttrs; 423 String clientName; 424 String clientMachine; 425 boolean overwrite; 426 byte storagePolicyId; 427 428 private AddCloseOp(FSEditLogOpCodes opCode) { 429 super(opCode); 430 storagePolicyId = BlockStoragePolicySuite.ID_UNSPECIFIED; 431 assert(opCode == OP_ADD || opCode == OP_CLOSE); 432 } 433 434 @Override 435 void resetSubFields() { 436 length = 0; 437 inodeId = 0L; 438 path = null; 439 replication = 0; 440 mtime = 0L; 441 atime = 0L; 442 blockSize = 0L; 443 blocks = null; 444 permissions = null; 445 aclEntries = null; 446 xAttrs = null; 447 clientName = null; 448 clientMachine = null; 449 overwrite = false; 450 storagePolicyId = 0; 451 } 452 453 <T extends AddCloseOp> T setInodeId(long inodeId) { 454 this.inodeId = inodeId; 455 return (T)this; 456 } 457 458 <T extends AddCloseOp> T setPath(String path) { 459 this.path = path; 460 return (T)this; 461 } 462 463 @Override 464 public String getPath() { 465 return path; 466 } 467 468 <T extends AddCloseOp> T setReplication(short replication) { 469 this.replication = replication; 470 return (T)this; 471 } 472 473 <T extends AddCloseOp> T setModificationTime(long mtime) { 474 this.mtime = mtime; 475 return (T)this; 476 } 477 478 <T extends AddCloseOp> T setAccessTime(long atime) { 479 this.atime = atime; 480 return (T)this; 481 } 482 483 <T extends AddCloseOp> T setBlockSize(long blockSize) { 484 this.blockSize = blockSize; 485 return (T)this; 486 } 487 488 <T extends AddCloseOp> T setBlocks(Block[] blocks) { 489 if (blocks.length > MAX_BLOCKS) { 490 throw new RuntimeException("Can't have more than " + MAX_BLOCKS + 491 " in an AddCloseOp."); 492 } 493 this.blocks = blocks; 494 return (T)this; 495 } 496 497 @Override 498 public Block[] getBlocks() { 499 return blocks; 500 } 501 502 <T extends AddCloseOp> T setPermissionStatus(PermissionStatus permissions) { 503 this.permissions = permissions; 504 return (T)this; 505 } 506 507 <T extends AddCloseOp> T setAclEntries(List<AclEntry> aclEntries) { 508 this.aclEntries = aclEntries; 509 return (T)this; 510 } 511 512 <T extends AddCloseOp> T setXAttrs(List<XAttr> xAttrs) { 513 this.xAttrs = xAttrs; 514 return (T)this; 515 } 516 517 <T extends AddCloseOp> T setClientName(String clientName) { 518 this.clientName = clientName; 519 return (T)this; 520 } 521 522 <T extends AddCloseOp> T setClientMachine(String clientMachine) { 523 this.clientMachine = clientMachine; 524 return (T)this; 525 } 526 527 <T extends AddCloseOp> T setOverwrite(boolean overwrite) { 528 this.overwrite = overwrite; 529 return (T)this; 530 } 531 532 <T extends AddCloseOp> T setStoragePolicyId(byte storagePolicyId) { 533 this.storagePolicyId = storagePolicyId; 534 return (T)this; 535 } 536 537 @Override 538 public void writeFields(DataOutputStream out) throws IOException { 539 FSImageSerialization.writeLong(inodeId, out); 540 FSImageSerialization.writeString(path, out); 541 FSImageSerialization.writeShort(replication, out); 542 FSImageSerialization.writeLong(mtime, out); 543 FSImageSerialization.writeLong(atime, out); 544 FSImageSerialization.writeLong(blockSize, out); 545 new ArrayWritable(Block.class, blocks).write(out); 546 permissions.write(out); 547 548 if (this.opCode == OP_ADD) { 549 AclEditLogUtil.write(aclEntries, out); 550 XAttrEditLogProto.Builder b = XAttrEditLogProto.newBuilder(); 551 b.addAllXAttrs(PBHelper.convertXAttrProto(xAttrs)); 552 b.build().writeDelimitedTo(out); 553 FSImageSerialization.writeString(clientName,out); 554 FSImageSerialization.writeString(clientMachine,out); 555 FSImageSerialization.writeBoolean(overwrite, out); 556 FSImageSerialization.writeByte(storagePolicyId, out); 557 // write clientId and callId 558 writeRpcIds(rpcClientId, rpcCallId, out); 559 } 560 } 561 562 @Override 563 void readFields(DataInputStream in, int logVersion) 564 throws IOException { 565 if (!NameNodeLayoutVersion.supports( 566 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 567 this.length = in.readInt(); 568 } 569 if (NameNodeLayoutVersion.supports( 570 LayoutVersion.Feature.ADD_INODE_ID, logVersion)) { 571 this.inodeId = in.readLong(); 572 } else { 573 // The inodeId should be updated when this editLogOp is applied 574 this.inodeId = INodeId.GRANDFATHER_INODE_ID; 575 } 576 if ((-17 < logVersion && length != 4) || 577 (logVersion <= -17 && length != 5 && !NameNodeLayoutVersion.supports( 578 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion))) { 579 throw new IOException("Incorrect data format." + 580 " logVersion is " + logVersion + 581 " but writables.length is " + 582 length + ". "); 583 } 584 this.path = FSImageSerialization.readString(in); 585 586 if (NameNodeLayoutVersion.supports( 587 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 588 this.replication = FSImageSerialization.readShort(in); 589 this.mtime = FSImageSerialization.readLong(in); 590 } else { 591 this.replication = readShort(in); 592 this.mtime = readLong(in); 593 } 594 595 if (NameNodeLayoutVersion.supports( 596 LayoutVersion.Feature.FILE_ACCESS_TIME, logVersion)) { 597 if (NameNodeLayoutVersion.supports( 598 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 599 this.atime = FSImageSerialization.readLong(in); 600 } else { 601 this.atime = readLong(in); 602 } 603 } else { 604 this.atime = 0; 605 } 606 607 if (NameNodeLayoutVersion.supports( 608 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 609 this.blockSize = FSImageSerialization.readLong(in); 610 } else { 611 this.blockSize = readLong(in); 612 } 613 614 this.blocks = readBlocks(in, logVersion); 615 this.permissions = PermissionStatus.read(in); 616 617 if (this.opCode == OP_ADD) { 618 aclEntries = AclEditLogUtil.read(in, logVersion); 619 this.xAttrs = readXAttrsFromEditLog(in, logVersion); 620 this.clientName = FSImageSerialization.readString(in); 621 this.clientMachine = FSImageSerialization.readString(in); 622 if (NameNodeLayoutVersion.supports( 623 NameNodeLayoutVersion.Feature.CREATE_OVERWRITE, logVersion)) { 624 this.overwrite = FSImageSerialization.readBoolean(in); 625 } else { 626 this.overwrite = false; 627 } 628 if (NameNodeLayoutVersion.supports( 629 NameNodeLayoutVersion.Feature.BLOCK_STORAGE_POLICY, logVersion)) { 630 this.storagePolicyId = FSImageSerialization.readByte(in); 631 } else { 632 this.storagePolicyId = BlockStoragePolicySuite.ID_UNSPECIFIED; 633 } 634 // read clientId and callId 635 readRpcIds(in, logVersion); 636 } else { 637 this.clientName = ""; 638 this.clientMachine = ""; 639 } 640 } 641 642 static final public int MAX_BLOCKS = 1024 * 1024 * 64; 643 644 private static Block[] readBlocks( 645 DataInputStream in, 646 int logVersion) throws IOException { 647 int numBlocks = in.readInt(); 648 if (numBlocks < 0) { 649 throw new IOException("invalid negative number of blocks"); 650 } else if (numBlocks > MAX_BLOCKS) { 651 throw new IOException("invalid number of blocks: " + numBlocks + 652 ". The maximum number of blocks per file is " + MAX_BLOCKS); 653 } 654 Block[] blocks = new Block[numBlocks]; 655 for (int i = 0; i < numBlocks; i++) { 656 Block blk = new Block(); 657 blk.readFields(in); 658 blocks[i] = blk; 659 } 660 return blocks; 661 } 662 663 public String stringifyMembers() { 664 StringBuilder builder = new StringBuilder(); 665 builder.append("[length="); 666 builder.append(length); 667 builder.append(", inodeId="); 668 builder.append(inodeId); 669 builder.append(", path="); 670 builder.append(path); 671 builder.append(", replication="); 672 builder.append(replication); 673 builder.append(", mtime="); 674 builder.append(mtime); 675 builder.append(", atime="); 676 builder.append(atime); 677 builder.append(", blockSize="); 678 builder.append(blockSize); 679 builder.append(", blocks="); 680 builder.append(Arrays.toString(blocks)); 681 builder.append(", permissions="); 682 builder.append(permissions); 683 builder.append(", aclEntries="); 684 builder.append(aclEntries); 685 builder.append(", clientName="); 686 builder.append(clientName); 687 builder.append(", clientMachine="); 688 builder.append(clientMachine); 689 builder.append(", overwrite="); 690 builder.append(overwrite); 691 if (this.opCode == OP_ADD) { 692 appendRpcIdsToString(builder, rpcClientId, rpcCallId); 693 } 694 builder.append(", storagePolicyId="); 695 builder.append(storagePolicyId); 696 builder.append(", opCode="); 697 builder.append(opCode); 698 builder.append(", txid="); 699 builder.append(txid); 700 builder.append("]"); 701 return builder.toString(); 702 } 703 704 @Override 705 protected void toXml(ContentHandler contentHandler) throws SAXException { 706 XMLUtils.addSaxString(contentHandler, "LENGTH", 707 Integer.toString(length)); 708 XMLUtils.addSaxString(contentHandler, "INODEID", 709 Long.toString(inodeId)); 710 XMLUtils.addSaxString(contentHandler, "PATH", path); 711 XMLUtils.addSaxString(contentHandler, "REPLICATION", 712 Short.valueOf(replication).toString()); 713 XMLUtils.addSaxString(contentHandler, "MTIME", 714 Long.toString(mtime)); 715 XMLUtils.addSaxString(contentHandler, "ATIME", 716 Long.toString(atime)); 717 XMLUtils.addSaxString(contentHandler, "BLOCKSIZE", 718 Long.toString(blockSize)); 719 XMLUtils.addSaxString(contentHandler, "CLIENT_NAME", clientName); 720 XMLUtils.addSaxString(contentHandler, "CLIENT_MACHINE", clientMachine); 721 XMLUtils.addSaxString(contentHandler, "OVERWRITE", 722 Boolean.toString(overwrite)); 723 for (Block b : blocks) { 724 FSEditLogOp.blockToXml(contentHandler, b); 725 } 726 FSEditLogOp.permissionStatusToXml(contentHandler, permissions); 727 if (this.opCode == OP_ADD) { 728 if (aclEntries != null) { 729 appendAclEntriesToXml(contentHandler, aclEntries); 730 } 731 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 732 } 733 } 734 735 @Override 736 void fromXml(Stanza st) throws InvalidXmlException { 737 this.length = Integer.parseInt(st.getValue("LENGTH")); 738 this.inodeId = Long.parseLong(st.getValue("INODEID")); 739 this.path = st.getValue("PATH"); 740 this.replication = Short.valueOf(st.getValue("REPLICATION")); 741 this.mtime = Long.parseLong(st.getValue("MTIME")); 742 this.atime = Long.parseLong(st.getValue("ATIME")); 743 this.blockSize = Long.parseLong(st.getValue("BLOCKSIZE")); 744 745 this.clientName = st.getValue("CLIENT_NAME"); 746 this.clientMachine = st.getValue("CLIENT_MACHINE"); 747 this.overwrite = Boolean.parseBoolean(st.getValueOrNull("OVERWRITE")); 748 if (st.hasChildren("BLOCK")) { 749 List<Stanza> blocks = st.getChildren("BLOCK"); 750 this.blocks = new Block[blocks.size()]; 751 for (int i = 0; i < blocks.size(); i++) { 752 this.blocks[i] = FSEditLogOp.blockFromXml(blocks.get(i)); 753 } 754 } else { 755 this.blocks = new Block[0]; 756 } 757 this.permissions = permissionStatusFromXml(st); 758 aclEntries = readAclEntriesFromXml(st); 759 readRpcIdsFromXml(st); 760 } 761 } 762 763 /** 764 * {@literal @AtMostOnce} for {@link ClientProtocol#create} and 765 * {@link ClientProtocol#append} 766 */ 767 static class AddOp extends AddCloseOp { 768 private AddOp() { 769 super(OP_ADD); 770 } 771 772 static AddOp getInstance(OpInstanceCache cache) { 773 return (AddOp)cache.get(OP_ADD); 774 } 775 776 @Override 777 public boolean shouldCompleteLastBlock() { 778 return false; 779 } 780 781 @Override 782 public String toString() { 783 StringBuilder builder = new StringBuilder(); 784 builder.append("AddOp "); 785 builder.append(stringifyMembers()); 786 return builder.toString(); 787 } 788 } 789 790 /** 791 * Although {@link ClientProtocol#appendFile} may also log a close op, we do 792 * not need to record the rpc ids here since a successful appendFile op will 793 * finally log an AddOp. 794 */ 795 static class CloseOp extends AddCloseOp { 796 private CloseOp() { 797 super(OP_CLOSE); 798 } 799 800 static CloseOp getInstance(OpInstanceCache cache) { 801 return (CloseOp)cache.get(OP_CLOSE); 802 } 803 804 @Override 805 public boolean shouldCompleteLastBlock() { 806 return true; 807 } 808 809 @Override 810 public String toString() { 811 StringBuilder builder = new StringBuilder(); 812 builder.append("CloseOp "); 813 builder.append(stringifyMembers()); 814 return builder.toString(); 815 } 816 } 817 818 static class AddBlockOp extends FSEditLogOp { 819 private String path; 820 private Block penultimateBlock; 821 private Block lastBlock; 822 823 private AddBlockOp() { 824 super(OP_ADD_BLOCK); 825 } 826 827 static AddBlockOp getInstance(OpInstanceCache cache) { 828 return (AddBlockOp) cache.get(OP_ADD_BLOCK); 829 } 830 831 @Override 832 void resetSubFields() { 833 path = null; 834 penultimateBlock = null; 835 lastBlock = null; 836 } 837 838 AddBlockOp setPath(String path) { 839 this.path = path; 840 return this; 841 } 842 843 public String getPath() { 844 return path; 845 } 846 847 AddBlockOp setPenultimateBlock(Block pBlock) { 848 this.penultimateBlock = pBlock; 849 return this; 850 } 851 852 Block getPenultimateBlock() { 853 return penultimateBlock; 854 } 855 856 AddBlockOp setLastBlock(Block lastBlock) { 857 this.lastBlock = lastBlock; 858 return this; 859 } 860 861 Block getLastBlock() { 862 return lastBlock; 863 } 864 865 @Override 866 public void writeFields(DataOutputStream out) throws IOException { 867 FSImageSerialization.writeString(path, out); 868 int size = penultimateBlock != null ? 2 : 1; 869 Block[] blocks = new Block[size]; 870 if (penultimateBlock != null) { 871 blocks[0] = penultimateBlock; 872 } 873 blocks[size - 1] = lastBlock; 874 FSImageSerialization.writeCompactBlockArray(blocks, out); 875 // clientId and callId 876 writeRpcIds(rpcClientId, rpcCallId, out); 877 } 878 879 @Override 880 void readFields(DataInputStream in, int logVersion) throws IOException { 881 path = FSImageSerialization.readString(in); 882 Block[] blocks = FSImageSerialization.readCompactBlockArray(in, 883 logVersion); 884 Preconditions.checkState(blocks.length == 2 || blocks.length == 1); 885 penultimateBlock = blocks.length == 1 ? null : blocks[0]; 886 lastBlock = blocks[blocks.length - 1]; 887 readRpcIds(in, logVersion); 888 } 889 890 @Override 891 public String toString() { 892 StringBuilder sb = new StringBuilder(); 893 sb.append("AddBlockOp [path=") 894 .append(path) 895 .append(", penultimateBlock=") 896 .append(penultimateBlock == null ? "NULL" : penultimateBlock) 897 .append(", lastBlock=") 898 .append(lastBlock); 899 appendRpcIdsToString(sb, rpcClientId, rpcCallId); 900 sb.append("]"); 901 return sb.toString(); 902 } 903 904 @Override 905 protected void toXml(ContentHandler contentHandler) throws SAXException { 906 XMLUtils.addSaxString(contentHandler, "PATH", path); 907 if (penultimateBlock != null) { 908 FSEditLogOp.blockToXml(contentHandler, penultimateBlock); 909 } 910 FSEditLogOp.blockToXml(contentHandler, lastBlock); 911 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 912 } 913 914 @Override 915 void fromXml(Stanza st) throws InvalidXmlException { 916 this.path = st.getValue("PATH"); 917 List<Stanza> blocks = st.getChildren("BLOCK"); 918 int size = blocks.size(); 919 Preconditions.checkState(size == 1 || size == 2); 920 this.penultimateBlock = size == 2 ? 921 FSEditLogOp.blockFromXml(blocks.get(0)) : null; 922 this.lastBlock = FSEditLogOp.blockFromXml(blocks.get(size - 1)); 923 readRpcIdsFromXml(st); 924 } 925 } 926 927 /** 928 * {@literal @AtMostOnce} for {@link ClientProtocol#updatePipeline}, but 929 * {@literal @Idempotent} for some other ops. 930 */ 931 static class UpdateBlocksOp extends FSEditLogOp implements BlockListUpdatingOp { 932 String path; 933 Block[] blocks; 934 935 private UpdateBlocksOp() { 936 super(OP_UPDATE_BLOCKS); 937 } 938 939 static UpdateBlocksOp getInstance(OpInstanceCache cache) { 940 return (UpdateBlocksOp)cache.get(OP_UPDATE_BLOCKS); 941 } 942 943 @Override 944 void resetSubFields() { 945 path = null; 946 blocks = null; 947 } 948 949 UpdateBlocksOp setPath(String path) { 950 this.path = path; 951 return this; 952 } 953 954 @Override 955 public String getPath() { 956 return path; 957 } 958 959 UpdateBlocksOp setBlocks(Block[] blocks) { 960 this.blocks = blocks; 961 return this; 962 } 963 964 @Override 965 public Block[] getBlocks() { 966 return blocks; 967 } 968 969 @Override 970 public 971 void writeFields(DataOutputStream out) throws IOException { 972 FSImageSerialization.writeString(path, out); 973 FSImageSerialization.writeCompactBlockArray(blocks, out); 974 // clientId and callId 975 writeRpcIds(rpcClientId, rpcCallId, out); 976 } 977 978 @Override 979 void readFields(DataInputStream in, int logVersion) throws IOException { 980 path = FSImageSerialization.readString(in); 981 this.blocks = FSImageSerialization.readCompactBlockArray( 982 in, logVersion); 983 readRpcIds(in, logVersion); 984 } 985 986 @Override 987 public boolean shouldCompleteLastBlock() { 988 return false; 989 } 990 991 @Override 992 public String toString() { 993 StringBuilder sb = new StringBuilder(); 994 sb.append("UpdateBlocksOp [path=") 995 .append(path) 996 .append(", blocks=") 997 .append(Arrays.toString(blocks)); 998 appendRpcIdsToString(sb, rpcClientId, rpcCallId); 999 sb.append("]"); 1000 return sb.toString(); 1001 } 1002 1003 @Override 1004 protected void toXml(ContentHandler contentHandler) throws SAXException { 1005 XMLUtils.addSaxString(contentHandler, "PATH", path); 1006 for (Block b : blocks) { 1007 FSEditLogOp.blockToXml(contentHandler, b); 1008 } 1009 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 1010 } 1011 1012 @Override void fromXml(Stanza st) throws InvalidXmlException { 1013 this.path = st.getValue("PATH"); 1014 List<Stanza> blocks = st.getChildren("BLOCK"); 1015 this.blocks = new Block[blocks.size()]; 1016 for (int i = 0; i < blocks.size(); i++) { 1017 this.blocks[i] = FSEditLogOp.blockFromXml(blocks.get(i)); 1018 } 1019 readRpcIdsFromXml(st); 1020 } 1021 } 1022 1023 /** {@literal @Idempotent} for {@link ClientProtocol#setReplication} */ 1024 static class SetReplicationOp extends FSEditLogOp { 1025 String path; 1026 short replication; 1027 1028 private SetReplicationOp() { 1029 super(OP_SET_REPLICATION); 1030 } 1031 1032 static SetReplicationOp getInstance(OpInstanceCache cache) { 1033 return (SetReplicationOp)cache.get(OP_SET_REPLICATION); 1034 } 1035 1036 @Override 1037 void resetSubFields() { 1038 path = null; 1039 replication = 0; 1040 } 1041 1042 SetReplicationOp setPath(String path) { 1043 this.path = path; 1044 return this; 1045 } 1046 1047 SetReplicationOp setReplication(short replication) { 1048 this.replication = replication; 1049 return this; 1050 } 1051 1052 @Override 1053 public 1054 void writeFields(DataOutputStream out) throws IOException { 1055 FSImageSerialization.writeString(path, out); 1056 FSImageSerialization.writeShort(replication, out); 1057 } 1058 1059 @Override 1060 void readFields(DataInputStream in, int logVersion) 1061 throws IOException { 1062 this.path = FSImageSerialization.readString(in); 1063 if (NameNodeLayoutVersion.supports( 1064 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 1065 this.replication = FSImageSerialization.readShort(in); 1066 } else { 1067 this.replication = readShort(in); 1068 } 1069 } 1070 1071 @Override 1072 public String toString() { 1073 StringBuilder builder = new StringBuilder(); 1074 builder.append("SetReplicationOp [path="); 1075 builder.append(path); 1076 builder.append(", replication="); 1077 builder.append(replication); 1078 builder.append(", opCode="); 1079 builder.append(opCode); 1080 builder.append(", txid="); 1081 builder.append(txid); 1082 builder.append("]"); 1083 return builder.toString(); 1084 } 1085 1086 @Override 1087 protected void toXml(ContentHandler contentHandler) throws SAXException { 1088 XMLUtils.addSaxString(contentHandler, "PATH", path); 1089 XMLUtils.addSaxString(contentHandler, "REPLICATION", 1090 Short.valueOf(replication).toString()); 1091 } 1092 1093 @Override void fromXml(Stanza st) throws InvalidXmlException { 1094 this.path = st.getValue("PATH"); 1095 this.replication = Short.valueOf(st.getValue("REPLICATION")); 1096 } 1097 } 1098 1099 /** {@literal @AtMostOnce} for {@link ClientProtocol#concat} */ 1100 static class ConcatDeleteOp extends FSEditLogOp { 1101 int length; 1102 String trg; 1103 String[] srcs; 1104 long timestamp; 1105 final static public int MAX_CONCAT_SRC = 1024 * 1024; 1106 1107 private ConcatDeleteOp() { 1108 super(OP_CONCAT_DELETE); 1109 } 1110 1111 static ConcatDeleteOp getInstance(OpInstanceCache cache) { 1112 return (ConcatDeleteOp)cache.get(OP_CONCAT_DELETE); 1113 } 1114 1115 @Override 1116 void resetSubFields() { 1117 length = 0; 1118 trg = null; 1119 srcs = null; 1120 timestamp = 0L; 1121 } 1122 1123 ConcatDeleteOp setTarget(String trg) { 1124 this.trg = trg; 1125 return this; 1126 } 1127 1128 ConcatDeleteOp setSources(String[] srcs) { 1129 if (srcs.length > MAX_CONCAT_SRC) { 1130 throw new RuntimeException("ConcatDeleteOp can only have " + 1131 MAX_CONCAT_SRC + " sources at most."); 1132 } 1133 this.srcs = srcs; 1134 1135 return this; 1136 } 1137 1138 ConcatDeleteOp setTimestamp(long timestamp) { 1139 this.timestamp = timestamp; 1140 return this; 1141 } 1142 1143 @Override 1144 public void writeFields(DataOutputStream out) throws IOException { 1145 FSImageSerialization.writeString(trg, out); 1146 1147 DeprecatedUTF8 info[] = new DeprecatedUTF8[srcs.length]; 1148 int idx = 0; 1149 for(int i=0; i<srcs.length; i++) { 1150 info[idx++] = new DeprecatedUTF8(srcs[i]); 1151 } 1152 new ArrayWritable(DeprecatedUTF8.class, info).write(out); 1153 1154 FSImageSerialization.writeLong(timestamp, out); 1155 1156 // rpc ids 1157 writeRpcIds(rpcClientId, rpcCallId, out); 1158 } 1159 1160 @Override 1161 void readFields(DataInputStream in, int logVersion) 1162 throws IOException { 1163 if (!NameNodeLayoutVersion.supports( 1164 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 1165 this.length = in.readInt(); 1166 if (length < 3) { // trg, srcs.., timestamp 1167 throw new IOException("Incorrect data format " + 1168 "for ConcatDeleteOp."); 1169 } 1170 } 1171 this.trg = FSImageSerialization.readString(in); 1172 int srcSize = 0; 1173 if (NameNodeLayoutVersion.supports( 1174 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 1175 srcSize = in.readInt(); 1176 } else { 1177 srcSize = this.length - 1 - 1; // trg and timestamp 1178 } 1179 if (srcSize < 0) { 1180 throw new IOException("Incorrect data format. " 1181 + "ConcatDeleteOp cannot have a negative number of data " + 1182 " sources."); 1183 } else if (srcSize > MAX_CONCAT_SRC) { 1184 throw new IOException("Incorrect data format. " 1185 + "ConcatDeleteOp can have at most " + MAX_CONCAT_SRC + 1186 " sources, but we tried to have " + (length - 3) + " sources."); 1187 } 1188 this.srcs = new String [srcSize]; 1189 for(int i=0; i<srcSize;i++) { 1190 srcs[i]= FSImageSerialization.readString(in); 1191 } 1192 1193 if (NameNodeLayoutVersion.supports( 1194 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 1195 this.timestamp = FSImageSerialization.readLong(in); 1196 } else { 1197 this.timestamp = readLong(in); 1198 } 1199 // read RPC ids if necessary 1200 readRpcIds(in, logVersion); 1201 } 1202 1203 @Override 1204 public String toString() { 1205 StringBuilder builder = new StringBuilder(); 1206 builder.append("ConcatDeleteOp [length="); 1207 builder.append(length); 1208 builder.append(", trg="); 1209 builder.append(trg); 1210 builder.append(", srcs="); 1211 builder.append(Arrays.toString(srcs)); 1212 builder.append(", timestamp="); 1213 builder.append(timestamp); 1214 appendRpcIdsToString(builder, rpcClientId, rpcCallId); 1215 builder.append(", opCode="); 1216 builder.append(opCode); 1217 builder.append(", txid="); 1218 builder.append(txid); 1219 builder.append("]"); 1220 return builder.toString(); 1221 } 1222 1223 @Override 1224 protected void toXml(ContentHandler contentHandler) throws SAXException { 1225 XMLUtils.addSaxString(contentHandler, "LENGTH", 1226 Integer.toString(length)); 1227 XMLUtils.addSaxString(contentHandler, "TRG", trg); 1228 XMLUtils.addSaxString(contentHandler, "TIMESTAMP", 1229 Long.toString(timestamp)); 1230 contentHandler.startElement("", "", "SOURCES", new AttributesImpl()); 1231 for (int i = 0; i < srcs.length; ++i) { 1232 XMLUtils.addSaxString(contentHandler, 1233 "SOURCE" + (i + 1), srcs[i]); 1234 } 1235 contentHandler.endElement("", "", "SOURCES"); 1236 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 1237 } 1238 1239 @Override void fromXml(Stanza st) throws InvalidXmlException { 1240 this.length = Integer.parseInt(st.getValue("LENGTH")); 1241 this.trg = st.getValue("TRG"); 1242 this.timestamp = Long.parseLong(st.getValue("TIMESTAMP")); 1243 List<Stanza> sources = st.getChildren("SOURCES"); 1244 int i = 0; 1245 while (true) { 1246 if (!sources.get(0).hasChildren("SOURCE" + (i + 1))) 1247 break; 1248 i++; 1249 } 1250 srcs = new String[i]; 1251 for (i = 0; i < srcs.length; i++) { 1252 srcs[i] = sources.get(0).getValue("SOURCE" + (i + 1)); 1253 } 1254 readRpcIdsFromXml(st); 1255 } 1256 } 1257 1258 /** {@literal @AtMostOnce} for {@link ClientProtocol#rename} */ 1259 static class RenameOldOp extends FSEditLogOp { 1260 int length; 1261 String src; 1262 String dst; 1263 long timestamp; 1264 1265 private RenameOldOp() { 1266 super(OP_RENAME_OLD); 1267 } 1268 1269 static RenameOldOp getInstance(OpInstanceCache cache) { 1270 return (RenameOldOp)cache.get(OP_RENAME_OLD); 1271 } 1272 1273 @Override 1274 void resetSubFields() { 1275 length = 0; 1276 src = null; 1277 dst = null; 1278 timestamp = 0L; 1279 } 1280 1281 RenameOldOp setSource(String src) { 1282 this.src = src; 1283 return this; 1284 } 1285 1286 RenameOldOp setDestination(String dst) { 1287 this.dst = dst; 1288 return this; 1289 } 1290 1291 RenameOldOp setTimestamp(long timestamp) { 1292 this.timestamp = timestamp; 1293 return this; 1294 } 1295 1296 @Override 1297 public 1298 void writeFields(DataOutputStream out) throws IOException { 1299 FSImageSerialization.writeString(src, out); 1300 FSImageSerialization.writeString(dst, out); 1301 FSImageSerialization.writeLong(timestamp, out); 1302 writeRpcIds(rpcClientId, rpcCallId, out); 1303 } 1304 1305 @Override 1306 void readFields(DataInputStream in, int logVersion) 1307 throws IOException { 1308 if (!NameNodeLayoutVersion.supports( 1309 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 1310 this.length = in.readInt(); 1311 if (this.length != 3) { 1312 throw new IOException("Incorrect data format. " 1313 + "Old rename operation."); 1314 } 1315 } 1316 this.src = FSImageSerialization.readString(in); 1317 this.dst = FSImageSerialization.readString(in); 1318 if (NameNodeLayoutVersion.supports( 1319 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 1320 this.timestamp = FSImageSerialization.readLong(in); 1321 } else { 1322 this.timestamp = readLong(in); 1323 } 1324 1325 // read RPC ids if necessary 1326 readRpcIds(in, logVersion); 1327 } 1328 1329 @Override 1330 public String toString() { 1331 StringBuilder builder = new StringBuilder(); 1332 builder.append("RenameOldOp [length="); 1333 builder.append(length); 1334 builder.append(", src="); 1335 builder.append(src); 1336 builder.append(", dst="); 1337 builder.append(dst); 1338 builder.append(", timestamp="); 1339 builder.append(timestamp); 1340 appendRpcIdsToString(builder, rpcClientId, rpcCallId); 1341 builder.append(", opCode="); 1342 builder.append(opCode); 1343 builder.append(", txid="); 1344 builder.append(txid); 1345 builder.append("]"); 1346 return builder.toString(); 1347 } 1348 1349 @Override 1350 protected void toXml(ContentHandler contentHandler) throws SAXException { 1351 XMLUtils.addSaxString(contentHandler, "LENGTH", 1352 Integer.toString(length)); 1353 XMLUtils.addSaxString(contentHandler, "SRC", src); 1354 XMLUtils.addSaxString(contentHandler, "DST", dst); 1355 XMLUtils.addSaxString(contentHandler, "TIMESTAMP", 1356 Long.toString(timestamp)); 1357 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 1358 } 1359 1360 @Override 1361 void fromXml(Stanza st) throws InvalidXmlException { 1362 this.length = Integer.parseInt(st.getValue("LENGTH")); 1363 this.src = st.getValue("SRC"); 1364 this.dst = st.getValue("DST"); 1365 this.timestamp = Long.parseLong(st.getValue("TIMESTAMP")); 1366 1367 readRpcIdsFromXml(st); 1368 } 1369 } 1370 1371 /** {@literal @AtMostOnce} for {@link ClientProtocol#delete} */ 1372 static class DeleteOp extends FSEditLogOp { 1373 int length; 1374 String path; 1375 long timestamp; 1376 1377 private DeleteOp() { 1378 super(OP_DELETE); 1379 } 1380 1381 static DeleteOp getInstance(OpInstanceCache cache) { 1382 return (DeleteOp)cache.get(OP_DELETE); 1383 } 1384 1385 @Override 1386 void resetSubFields() { 1387 length = 0; 1388 path = null; 1389 timestamp = 0L; 1390 } 1391 1392 DeleteOp setPath(String path) { 1393 this.path = path; 1394 return this; 1395 } 1396 1397 DeleteOp setTimestamp(long timestamp) { 1398 this.timestamp = timestamp; 1399 return this; 1400 } 1401 1402 @Override 1403 public 1404 void writeFields(DataOutputStream out) throws IOException { 1405 FSImageSerialization.writeString(path, out); 1406 FSImageSerialization.writeLong(timestamp, out); 1407 writeRpcIds(rpcClientId, rpcCallId, out); 1408 } 1409 1410 @Override 1411 void readFields(DataInputStream in, int logVersion) 1412 throws IOException { 1413 if (!NameNodeLayoutVersion.supports( 1414 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 1415 this.length = in.readInt(); 1416 if (this.length != 2) { 1417 throw new IOException("Incorrect data format. " + "delete operation."); 1418 } 1419 } 1420 this.path = FSImageSerialization.readString(in); 1421 if (NameNodeLayoutVersion.supports( 1422 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 1423 this.timestamp = FSImageSerialization.readLong(in); 1424 } else { 1425 this.timestamp = readLong(in); 1426 } 1427 // read RPC ids if necessary 1428 readRpcIds(in, logVersion); 1429 } 1430 1431 @Override 1432 public String toString() { 1433 StringBuilder builder = new StringBuilder(); 1434 builder.append("DeleteOp [length="); 1435 builder.append(length); 1436 builder.append(", path="); 1437 builder.append(path); 1438 builder.append(", timestamp="); 1439 builder.append(timestamp); 1440 appendRpcIdsToString(builder, rpcClientId, rpcCallId); 1441 builder.append(", opCode="); 1442 builder.append(opCode); 1443 builder.append(", txid="); 1444 builder.append(txid); 1445 builder.append("]"); 1446 return builder.toString(); 1447 } 1448 1449 @Override 1450 protected void toXml(ContentHandler contentHandler) throws SAXException { 1451 XMLUtils.addSaxString(contentHandler, "LENGTH", 1452 Integer.toString(length)); 1453 XMLUtils.addSaxString(contentHandler, "PATH", path); 1454 XMLUtils.addSaxString(contentHandler, "TIMESTAMP", 1455 Long.toString(timestamp)); 1456 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 1457 } 1458 1459 @Override void fromXml(Stanza st) throws InvalidXmlException { 1460 this.length = Integer.parseInt(st.getValue("LENGTH")); 1461 this.path = st.getValue("PATH"); 1462 this.timestamp = Long.parseLong(st.getValue("TIMESTAMP")); 1463 1464 readRpcIdsFromXml(st); 1465 } 1466 } 1467 1468 /** {@literal @Idempotent} for {@link ClientProtocol#mkdirs} */ 1469 static class MkdirOp extends FSEditLogOp { 1470 int length; 1471 long inodeId; 1472 String path; 1473 long timestamp; 1474 PermissionStatus permissions; 1475 List<AclEntry> aclEntries; 1476 List<XAttr> xAttrs; 1477 1478 private MkdirOp() { 1479 super(OP_MKDIR); 1480 } 1481 1482 static MkdirOp getInstance(OpInstanceCache cache) { 1483 return (MkdirOp)cache.get(OP_MKDIR); 1484 } 1485 1486 @Override 1487 void resetSubFields() { 1488 length = 0; 1489 inodeId = 0L; 1490 path = null; 1491 timestamp = 0L; 1492 permissions = null; 1493 aclEntries = null; 1494 xAttrs = null; 1495 } 1496 1497 MkdirOp setInodeId(long inodeId) { 1498 this.inodeId = inodeId; 1499 return this; 1500 } 1501 1502 MkdirOp setPath(String path) { 1503 this.path = path; 1504 return this; 1505 } 1506 1507 MkdirOp setTimestamp(long timestamp) { 1508 this.timestamp = timestamp; 1509 return this; 1510 } 1511 1512 MkdirOp setPermissionStatus(PermissionStatus permissions) { 1513 this.permissions = permissions; 1514 return this; 1515 } 1516 1517 MkdirOp setAclEntries(List<AclEntry> aclEntries) { 1518 this.aclEntries = aclEntries; 1519 return this; 1520 } 1521 1522 MkdirOp setXAttrs(List<XAttr> xAttrs) { 1523 this.xAttrs = xAttrs; 1524 return this; 1525 } 1526 1527 @Override 1528 public 1529 void writeFields(DataOutputStream out) throws IOException { 1530 FSImageSerialization.writeLong(inodeId, out); 1531 FSImageSerialization.writeString(path, out); 1532 FSImageSerialization.writeLong(timestamp, out); // mtime 1533 FSImageSerialization.writeLong(timestamp, out); // atime, unused at this 1534 permissions.write(out); 1535 AclEditLogUtil.write(aclEntries, out); 1536 XAttrEditLogProto.Builder b = XAttrEditLogProto.newBuilder(); 1537 b.addAllXAttrs(PBHelper.convertXAttrProto(xAttrs)); 1538 b.build().writeDelimitedTo(out); 1539 } 1540 1541 @Override 1542 void readFields(DataInputStream in, int logVersion) throws IOException { 1543 if (!NameNodeLayoutVersion.supports( 1544 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 1545 this.length = in.readInt(); 1546 } 1547 if (-17 < logVersion && length != 2 || 1548 logVersion <= -17 && length != 3 1549 && !NameNodeLayoutVersion.supports( 1550 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 1551 throw new IOException("Incorrect data format. Mkdir operation."); 1552 } 1553 if (NameNodeLayoutVersion.supports( 1554 LayoutVersion.Feature.ADD_INODE_ID, logVersion)) { 1555 this.inodeId = FSImageSerialization.readLong(in); 1556 } else { 1557 // This id should be updated when this editLogOp is applied 1558 this.inodeId = INodeId.GRANDFATHER_INODE_ID; 1559 } 1560 this.path = FSImageSerialization.readString(in); 1561 if (NameNodeLayoutVersion.supports( 1562 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 1563 this.timestamp = FSImageSerialization.readLong(in); 1564 } else { 1565 this.timestamp = readLong(in); 1566 } 1567 1568 // The disk format stores atimes for directories as well. 1569 // However, currently this is not being updated/used because of 1570 // performance reasons. 1571 if (NameNodeLayoutVersion.supports( 1572 LayoutVersion.Feature.FILE_ACCESS_TIME, logVersion)) { 1573 if (NameNodeLayoutVersion.supports( 1574 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 1575 FSImageSerialization.readLong(in); 1576 } else { 1577 readLong(in); 1578 } 1579 } 1580 1581 this.permissions = PermissionStatus.read(in); 1582 aclEntries = AclEditLogUtil.read(in, logVersion); 1583 1584 xAttrs = readXAttrsFromEditLog(in, logVersion); 1585 } 1586 1587 @Override 1588 public String toString() { 1589 StringBuilder builder = new StringBuilder(); 1590 builder.append("MkdirOp [length="); 1591 builder.append(length); 1592 builder.append(", inodeId="); 1593 builder.append(inodeId); 1594 builder.append(", path="); 1595 builder.append(path); 1596 builder.append(", timestamp="); 1597 builder.append(timestamp); 1598 builder.append(", permissions="); 1599 builder.append(permissions); 1600 builder.append(", aclEntries="); 1601 builder.append(aclEntries); 1602 builder.append(", opCode="); 1603 builder.append(opCode); 1604 builder.append(", txid="); 1605 builder.append(txid); 1606 builder.append(", xAttrs="); 1607 builder.append(xAttrs); 1608 builder.append("]"); 1609 return builder.toString(); 1610 } 1611 1612 @Override 1613 protected void toXml(ContentHandler contentHandler) throws SAXException { 1614 XMLUtils.addSaxString(contentHandler, "LENGTH", 1615 Integer.toString(length)); 1616 XMLUtils.addSaxString(contentHandler, "INODEID", 1617 Long.toString(inodeId)); 1618 XMLUtils.addSaxString(contentHandler, "PATH", path); 1619 XMLUtils.addSaxString(contentHandler, "TIMESTAMP", 1620 Long.toString(timestamp)); 1621 FSEditLogOp.permissionStatusToXml(contentHandler, permissions); 1622 if (aclEntries != null) { 1623 appendAclEntriesToXml(contentHandler, aclEntries); 1624 } 1625 if (xAttrs != null) { 1626 appendXAttrsToXml(contentHandler, xAttrs); 1627 } 1628 } 1629 1630 @Override void fromXml(Stanza st) throws InvalidXmlException { 1631 this.length = Integer.parseInt(st.getValue("LENGTH")); 1632 this.inodeId = Long.parseLong(st.getValue("INODEID")); 1633 this.path = st.getValue("PATH"); 1634 this.timestamp = Long.parseLong(st.getValue("TIMESTAMP")); 1635 this.permissions = permissionStatusFromXml(st); 1636 aclEntries = readAclEntriesFromXml(st); 1637 xAttrs = readXAttrsFromXml(st); 1638 } 1639 } 1640 1641 /** 1642 * The corresponding operations are either {@literal @Idempotent} ( 1643 * {@link ClientProtocol#updateBlockForPipeline}, 1644 * {@link ClientProtocol#recoverLease}, {@link ClientProtocol#addBlock}) or 1645 * already bound with other editlog op which records rpc ids ( 1646 * {@link ClientProtocol#startFile}). Thus no need to record rpc ids here. 1647 */ 1648 static class SetGenstampV1Op extends FSEditLogOp { 1649 long genStampV1; 1650 1651 private SetGenstampV1Op() { 1652 super(OP_SET_GENSTAMP_V1); 1653 } 1654 1655 static SetGenstampV1Op getInstance(OpInstanceCache cache) { 1656 return (SetGenstampV1Op)cache.get(OP_SET_GENSTAMP_V1); 1657 } 1658 1659 @Override 1660 void resetSubFields() { 1661 genStampV1 = 0L; 1662 } 1663 1664 SetGenstampV1Op setGenerationStamp(long genStamp) { 1665 this.genStampV1 = genStamp; 1666 return this; 1667 } 1668 1669 @Override 1670 public 1671 void writeFields(DataOutputStream out) throws IOException { 1672 FSImageSerialization.writeLong(genStampV1, out); 1673 } 1674 1675 @Override 1676 void readFields(DataInputStream in, int logVersion) 1677 throws IOException { 1678 this.genStampV1 = FSImageSerialization.readLong(in); 1679 } 1680 1681 @Override 1682 public String toString() { 1683 StringBuilder builder = new StringBuilder(); 1684 builder.append("SetGenstampOp [GenStamp="); 1685 builder.append(genStampV1); 1686 builder.append(", opCode="); 1687 builder.append(opCode); 1688 builder.append(", txid="); 1689 builder.append(txid); 1690 builder.append("]"); 1691 return builder.toString(); 1692 } 1693 1694 @Override 1695 protected void toXml(ContentHandler contentHandler) throws SAXException { 1696 XMLUtils.addSaxString(contentHandler, "GENSTAMP", 1697 Long.toString(genStampV1)); 1698 } 1699 1700 @Override void fromXml(Stanza st) throws InvalidXmlException { 1701 this.genStampV1 = Long.parseLong(st.getValue("GENSTAMP")); 1702 } 1703 } 1704 1705 /** Similar with {@link SetGenstampV1Op} */ 1706 static class SetGenstampV2Op extends FSEditLogOp { 1707 long genStampV2; 1708 1709 private SetGenstampV2Op() { 1710 super(OP_SET_GENSTAMP_V2); 1711 } 1712 1713 static SetGenstampV2Op getInstance(OpInstanceCache cache) { 1714 return (SetGenstampV2Op)cache.get(OP_SET_GENSTAMP_V2); 1715 } 1716 1717 @Override 1718 void resetSubFields() { 1719 genStampV2 = 0L; 1720 } 1721 1722 SetGenstampV2Op setGenerationStamp(long genStamp) { 1723 this.genStampV2 = genStamp; 1724 return this; 1725 } 1726 1727 @Override 1728 public 1729 void writeFields(DataOutputStream out) throws IOException { 1730 FSImageSerialization.writeLong(genStampV2, out); 1731 } 1732 1733 @Override 1734 void readFields(DataInputStream in, int logVersion) 1735 throws IOException { 1736 this.genStampV2 = FSImageSerialization.readLong(in); 1737 } 1738 1739 @Override 1740 public String toString() { 1741 StringBuilder builder = new StringBuilder(); 1742 builder.append("SetGenstampV2Op [GenStampV2="); 1743 builder.append(genStampV2); 1744 builder.append(", opCode="); 1745 builder.append(opCode); 1746 builder.append(", txid="); 1747 builder.append(txid); 1748 builder.append("]"); 1749 return builder.toString(); 1750 } 1751 1752 @Override 1753 protected void toXml(ContentHandler contentHandler) throws SAXException { 1754 XMLUtils.addSaxString(contentHandler, "GENSTAMPV2", 1755 Long.toString(genStampV2)); 1756 } 1757 1758 @Override void fromXml(Stanza st) throws InvalidXmlException { 1759 this.genStampV2 = Long.parseLong(st.getValue("GENSTAMPV2")); 1760 } 1761 } 1762 1763 /** {@literal @Idempotent} for {@link ClientProtocol#addBlock} */ 1764 static class AllocateBlockIdOp extends FSEditLogOp { 1765 long blockId; 1766 1767 private AllocateBlockIdOp() { 1768 super(OP_ALLOCATE_BLOCK_ID); 1769 } 1770 1771 static AllocateBlockIdOp getInstance(OpInstanceCache cache) { 1772 return (AllocateBlockIdOp)cache.get(OP_ALLOCATE_BLOCK_ID); 1773 } 1774 1775 @Override 1776 void resetSubFields() { 1777 blockId = 0L; 1778 } 1779 1780 AllocateBlockIdOp setBlockId(long blockId) { 1781 this.blockId = blockId; 1782 return this; 1783 } 1784 1785 @Override 1786 public 1787 void writeFields(DataOutputStream out) throws IOException { 1788 FSImageSerialization.writeLong(blockId, out); 1789 } 1790 1791 @Override 1792 void readFields(DataInputStream in, int logVersion) 1793 throws IOException { 1794 this.blockId = FSImageSerialization.readLong(in); 1795 } 1796 1797 @Override 1798 public String toString() { 1799 StringBuilder builder = new StringBuilder(); 1800 builder.append("AllocateBlockIdOp [blockId="); 1801 builder.append(blockId); 1802 builder.append(", opCode="); 1803 builder.append(opCode); 1804 builder.append(", txid="); 1805 builder.append(txid); 1806 builder.append("]"); 1807 return builder.toString(); 1808 } 1809 1810 @Override 1811 protected void toXml(ContentHandler contentHandler) throws SAXException { 1812 XMLUtils.addSaxString(contentHandler, "BLOCK_ID", 1813 Long.toString(blockId)); 1814 } 1815 1816 @Override void fromXml(Stanza st) throws InvalidXmlException { 1817 this.blockId = Long.parseLong(st.getValue("BLOCK_ID")); 1818 } 1819 } 1820 1821 /** {@literal @Idempotent} for {@link ClientProtocol#setPermission} */ 1822 static class SetPermissionsOp extends FSEditLogOp { 1823 String src; 1824 FsPermission permissions; 1825 1826 private SetPermissionsOp() { 1827 super(OP_SET_PERMISSIONS); 1828 } 1829 1830 static SetPermissionsOp getInstance(OpInstanceCache cache) { 1831 return (SetPermissionsOp)cache.get(OP_SET_PERMISSIONS); 1832 } 1833 1834 @Override 1835 void resetSubFields() { 1836 src = null; 1837 permissions = null; 1838 } 1839 1840 SetPermissionsOp setSource(String src) { 1841 this.src = src; 1842 return this; 1843 } 1844 1845 SetPermissionsOp setPermissions(FsPermission permissions) { 1846 this.permissions = permissions; 1847 return this; 1848 } 1849 1850 @Override 1851 public 1852 void writeFields(DataOutputStream out) throws IOException { 1853 FSImageSerialization.writeString(src, out); 1854 permissions.write(out); 1855 } 1856 1857 @Override 1858 void readFields(DataInputStream in, int logVersion) 1859 throws IOException { 1860 this.src = FSImageSerialization.readString(in); 1861 this.permissions = FsPermission.read(in); 1862 } 1863 1864 @Override 1865 public String toString() { 1866 StringBuilder builder = new StringBuilder(); 1867 builder.append("SetPermissionsOp [src="); 1868 builder.append(src); 1869 builder.append(", permissions="); 1870 builder.append(permissions); 1871 builder.append(", opCode="); 1872 builder.append(opCode); 1873 builder.append(", txid="); 1874 builder.append(txid); 1875 builder.append("]"); 1876 return builder.toString(); 1877 } 1878 1879 @Override 1880 protected void toXml(ContentHandler contentHandler) throws SAXException { 1881 XMLUtils.addSaxString(contentHandler, "SRC", src); 1882 XMLUtils.addSaxString(contentHandler, "MODE", 1883 Short.valueOf(permissions.toShort()).toString()); 1884 } 1885 1886 @Override void fromXml(Stanza st) throws InvalidXmlException { 1887 this.src = st.getValue("SRC"); 1888 this.permissions = new FsPermission( 1889 Short.valueOf(st.getValue("MODE"))); 1890 } 1891 } 1892 1893 /** {@literal @Idempotent} for {@link ClientProtocol#setOwner} */ 1894 static class SetOwnerOp extends FSEditLogOp { 1895 String src; 1896 String username; 1897 String groupname; 1898 1899 private SetOwnerOp() { 1900 super(OP_SET_OWNER); 1901 } 1902 1903 static SetOwnerOp getInstance(OpInstanceCache cache) { 1904 return (SetOwnerOp)cache.get(OP_SET_OWNER); 1905 } 1906 1907 @Override 1908 void resetSubFields() { 1909 src = null; 1910 username = null; 1911 groupname = null; 1912 } 1913 1914 SetOwnerOp setSource(String src) { 1915 this.src = src; 1916 return this; 1917 } 1918 1919 SetOwnerOp setUser(String username) { 1920 this.username = username; 1921 return this; 1922 } 1923 1924 SetOwnerOp setGroup(String groupname) { 1925 this.groupname = groupname; 1926 return this; 1927 } 1928 1929 @Override 1930 public 1931 void writeFields(DataOutputStream out) throws IOException { 1932 FSImageSerialization.writeString(src, out); 1933 FSImageSerialization.writeString(username == null ? "" : username, out); 1934 FSImageSerialization.writeString(groupname == null ? "" : groupname, out); 1935 } 1936 1937 @Override 1938 void readFields(DataInputStream in, int logVersion) 1939 throws IOException { 1940 this.src = FSImageSerialization.readString(in); 1941 this.username = FSImageSerialization.readString_EmptyAsNull(in); 1942 this.groupname = FSImageSerialization.readString_EmptyAsNull(in); 1943 } 1944 1945 @Override 1946 public String toString() { 1947 StringBuilder builder = new StringBuilder(); 1948 builder.append("SetOwnerOp [src="); 1949 builder.append(src); 1950 builder.append(", username="); 1951 builder.append(username); 1952 builder.append(", groupname="); 1953 builder.append(groupname); 1954 builder.append(", opCode="); 1955 builder.append(opCode); 1956 builder.append(", txid="); 1957 builder.append(txid); 1958 builder.append("]"); 1959 return builder.toString(); 1960 } 1961 1962 @Override 1963 protected void toXml(ContentHandler contentHandler) throws SAXException { 1964 XMLUtils.addSaxString(contentHandler, "SRC", src); 1965 if (username != null) { 1966 XMLUtils.addSaxString(contentHandler, "USERNAME", username); 1967 } 1968 if (groupname != null) { 1969 XMLUtils.addSaxString(contentHandler, "GROUPNAME", groupname); 1970 } 1971 } 1972 1973 @Override void fromXml(Stanza st) throws InvalidXmlException { 1974 this.src = st.getValue("SRC"); 1975 this.username = (st.hasChildren("USERNAME")) ? 1976 st.getValue("USERNAME") : null; 1977 this.groupname = (st.hasChildren("GROUPNAME")) ? 1978 st.getValue("GROUPNAME") : null; 1979 } 1980 } 1981 1982 static class SetNSQuotaOp extends FSEditLogOp { 1983 String src; 1984 long nsQuota; 1985 1986 private SetNSQuotaOp() { 1987 super(OP_SET_NS_QUOTA); 1988 } 1989 1990 static SetNSQuotaOp getInstance(OpInstanceCache cache) { 1991 return (SetNSQuotaOp)cache.get(OP_SET_NS_QUOTA); 1992 } 1993 1994 @Override 1995 void resetSubFields() { 1996 src = null; 1997 nsQuota = 0L; 1998 } 1999 2000 @Override 2001 public 2002 void writeFields(DataOutputStream out) throws IOException { 2003 throw new IOException("Deprecated"); 2004 } 2005 2006 @Override 2007 void readFields(DataInputStream in, int logVersion) 2008 throws IOException { 2009 this.src = FSImageSerialization.readString(in); 2010 this.nsQuota = FSImageSerialization.readLong(in); 2011 } 2012 2013 @Override 2014 public String toString() { 2015 StringBuilder builder = new StringBuilder(); 2016 builder.append("SetNSQuotaOp [src="); 2017 builder.append(src); 2018 builder.append(", nsQuota="); 2019 builder.append(nsQuota); 2020 builder.append(", opCode="); 2021 builder.append(opCode); 2022 builder.append(", txid="); 2023 builder.append(txid); 2024 builder.append("]"); 2025 return builder.toString(); 2026 } 2027 2028 @Override 2029 protected void toXml(ContentHandler contentHandler) throws SAXException { 2030 XMLUtils.addSaxString(contentHandler, "SRC", src); 2031 XMLUtils.addSaxString(contentHandler, "NSQUOTA", 2032 Long.toString(nsQuota)); 2033 } 2034 2035 @Override void fromXml(Stanza st) throws InvalidXmlException { 2036 this.src = st.getValue("SRC"); 2037 this.nsQuota = Long.parseLong(st.getValue("NSQUOTA")); 2038 } 2039 } 2040 2041 static class ClearNSQuotaOp extends FSEditLogOp { 2042 String src; 2043 2044 private ClearNSQuotaOp() { 2045 super(OP_CLEAR_NS_QUOTA); 2046 } 2047 2048 static ClearNSQuotaOp getInstance(OpInstanceCache cache) { 2049 return (ClearNSQuotaOp)cache.get(OP_CLEAR_NS_QUOTA); 2050 } 2051 2052 @Override 2053 void resetSubFields() { 2054 src = null; 2055 } 2056 2057 @Override 2058 public 2059 void writeFields(DataOutputStream out) throws IOException { 2060 throw new IOException("Deprecated"); 2061 } 2062 2063 @Override 2064 void readFields(DataInputStream in, int logVersion) 2065 throws IOException { 2066 this.src = FSImageSerialization.readString(in); 2067 } 2068 2069 @Override 2070 public String toString() { 2071 StringBuilder builder = new StringBuilder(); 2072 builder.append("ClearNSQuotaOp [src="); 2073 builder.append(src); 2074 builder.append(", opCode="); 2075 builder.append(opCode); 2076 builder.append(", txid="); 2077 builder.append(txid); 2078 builder.append("]"); 2079 return builder.toString(); 2080 } 2081 2082 @Override 2083 protected void toXml(ContentHandler contentHandler) throws SAXException { 2084 XMLUtils.addSaxString(contentHandler, "SRC", src); 2085 } 2086 2087 @Override void fromXml(Stanza st) throws InvalidXmlException { 2088 this.src = st.getValue("SRC"); 2089 } 2090 } 2091 2092 /** {@literal @Idempotent} for {@link ClientProtocol#setQuota} */ 2093 static class SetQuotaOp extends FSEditLogOp { 2094 String src; 2095 long nsQuota; 2096 long dsQuota; 2097 2098 private SetQuotaOp() { 2099 super(OP_SET_QUOTA); 2100 } 2101 2102 static SetQuotaOp getInstance(OpInstanceCache cache) { 2103 return (SetQuotaOp)cache.get(OP_SET_QUOTA); 2104 } 2105 2106 @Override 2107 void resetSubFields() { 2108 src = null; 2109 nsQuota = 0L; 2110 dsQuota = 0L; 2111 } 2112 2113 SetQuotaOp setSource(String src) { 2114 this.src = src; 2115 return this; 2116 } 2117 2118 SetQuotaOp setNSQuota(long nsQuota) { 2119 this.nsQuota = nsQuota; 2120 return this; 2121 } 2122 2123 SetQuotaOp setDSQuota(long dsQuota) { 2124 this.dsQuota = dsQuota; 2125 return this; 2126 } 2127 2128 @Override 2129 public 2130 void writeFields(DataOutputStream out) throws IOException { 2131 FSImageSerialization.writeString(src, out); 2132 FSImageSerialization.writeLong(nsQuota, out); 2133 FSImageSerialization.writeLong(dsQuota, out); 2134 } 2135 2136 @Override 2137 void readFields(DataInputStream in, int logVersion) 2138 throws IOException { 2139 this.src = FSImageSerialization.readString(in); 2140 this.nsQuota = FSImageSerialization.readLong(in); 2141 this.dsQuota = FSImageSerialization.readLong(in); 2142 } 2143 2144 @Override 2145 public String toString() { 2146 StringBuilder builder = new StringBuilder(); 2147 builder.append("SetQuotaOp [src="); 2148 builder.append(src); 2149 builder.append(", nsQuota="); 2150 builder.append(nsQuota); 2151 builder.append(", dsQuota="); 2152 builder.append(dsQuota); 2153 builder.append(", opCode="); 2154 builder.append(opCode); 2155 builder.append(", txid="); 2156 builder.append(txid); 2157 builder.append("]"); 2158 return builder.toString(); 2159 } 2160 2161 @Override 2162 protected void toXml(ContentHandler contentHandler) throws SAXException { 2163 XMLUtils.addSaxString(contentHandler, "SRC", src); 2164 XMLUtils.addSaxString(contentHandler, "NSQUOTA", 2165 Long.toString(nsQuota)); 2166 XMLUtils.addSaxString(contentHandler, "DSQUOTA", 2167 Long.toString(dsQuota)); 2168 } 2169 2170 @Override void fromXml(Stanza st) throws InvalidXmlException { 2171 this.src = st.getValue("SRC"); 2172 this.nsQuota = Long.parseLong(st.getValue("NSQUOTA")); 2173 this.dsQuota = Long.parseLong(st.getValue("DSQUOTA")); 2174 } 2175 } 2176 2177 /** {@literal @Idempotent} for {@link ClientProtocol#setTimes} */ 2178 static class TimesOp extends FSEditLogOp { 2179 int length; 2180 String path; 2181 long mtime; 2182 long atime; 2183 2184 private TimesOp() { 2185 super(OP_TIMES); 2186 } 2187 2188 static TimesOp getInstance(OpInstanceCache cache) { 2189 return (TimesOp)cache.get(OP_TIMES); 2190 } 2191 2192 @Override 2193 void resetSubFields() { 2194 length = 0; 2195 path = null; 2196 mtime = 0L; 2197 atime = 0L; 2198 } 2199 2200 TimesOp setPath(String path) { 2201 this.path = path; 2202 return this; 2203 } 2204 2205 TimesOp setModificationTime(long mtime) { 2206 this.mtime = mtime; 2207 return this; 2208 } 2209 2210 TimesOp setAccessTime(long atime) { 2211 this.atime = atime; 2212 return this; 2213 } 2214 2215 @Override 2216 public 2217 void writeFields(DataOutputStream out) throws IOException { 2218 FSImageSerialization.writeString(path, out); 2219 FSImageSerialization.writeLong(mtime, out); 2220 FSImageSerialization.writeLong(atime, out); 2221 } 2222 2223 @Override 2224 void readFields(DataInputStream in, int logVersion) 2225 throws IOException { 2226 if (!NameNodeLayoutVersion.supports( 2227 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 2228 this.length = in.readInt(); 2229 if (length != 3) { 2230 throw new IOException("Incorrect data format. " + "times operation."); 2231 } 2232 } 2233 this.path = FSImageSerialization.readString(in); 2234 2235 if (NameNodeLayoutVersion.supports( 2236 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 2237 this.mtime = FSImageSerialization.readLong(in); 2238 this.atime = FSImageSerialization.readLong(in); 2239 } else { 2240 this.mtime = readLong(in); 2241 this.atime = readLong(in); 2242 } 2243 } 2244 2245 @Override 2246 public String toString() { 2247 StringBuilder builder = new StringBuilder(); 2248 builder.append("TimesOp [length="); 2249 builder.append(length); 2250 builder.append(", path="); 2251 builder.append(path); 2252 builder.append(", mtime="); 2253 builder.append(mtime); 2254 builder.append(", atime="); 2255 builder.append(atime); 2256 builder.append(", opCode="); 2257 builder.append(opCode); 2258 builder.append(", txid="); 2259 builder.append(txid); 2260 builder.append("]"); 2261 return builder.toString(); 2262 } 2263 2264 @Override 2265 protected void toXml(ContentHandler contentHandler) throws SAXException { 2266 XMLUtils.addSaxString(contentHandler, "LENGTH", 2267 Integer.toString(length)); 2268 XMLUtils.addSaxString(contentHandler, "PATH", path); 2269 XMLUtils.addSaxString(contentHandler, "MTIME", 2270 Long.toString(mtime)); 2271 XMLUtils.addSaxString(contentHandler, "ATIME", 2272 Long.toString(atime)); 2273 } 2274 2275 @Override void fromXml(Stanza st) throws InvalidXmlException { 2276 this.length = Integer.parseInt(st.getValue("LENGTH")); 2277 this.path = st.getValue("PATH"); 2278 this.mtime = Long.parseLong(st.getValue("MTIME")); 2279 this.atime = Long.parseLong(st.getValue("ATIME")); 2280 } 2281 } 2282 2283 /** {@literal @AtMostOnce} for {@link ClientProtocol#createSymlink} */ 2284 static class SymlinkOp extends FSEditLogOp { 2285 int length; 2286 long inodeId; 2287 String path; 2288 String value; 2289 long mtime; 2290 long atime; 2291 PermissionStatus permissionStatus; 2292 2293 private SymlinkOp() { 2294 super(OP_SYMLINK); 2295 } 2296 2297 static SymlinkOp getInstance(OpInstanceCache cache) { 2298 return (SymlinkOp)cache.get(OP_SYMLINK); 2299 } 2300 2301 @Override 2302 void resetSubFields() { 2303 length = 0; 2304 inodeId = 0L; 2305 path = null; 2306 value = null; 2307 mtime = 0L; 2308 atime = 0L; 2309 permissionStatus = null; 2310 } 2311 2312 SymlinkOp setId(long inodeId) { 2313 this.inodeId = inodeId; 2314 return this; 2315 } 2316 2317 SymlinkOp setPath(String path) { 2318 this.path = path; 2319 return this; 2320 } 2321 2322 SymlinkOp setValue(String value) { 2323 this.value = value; 2324 return this; 2325 } 2326 2327 SymlinkOp setModificationTime(long mtime) { 2328 this.mtime = mtime; 2329 return this; 2330 } 2331 2332 SymlinkOp setAccessTime(long atime) { 2333 this.atime = atime; 2334 return this; 2335 } 2336 2337 SymlinkOp setPermissionStatus(PermissionStatus permissionStatus) { 2338 this.permissionStatus = permissionStatus; 2339 return this; 2340 } 2341 2342 @Override 2343 public void writeFields(DataOutputStream out) throws IOException { 2344 FSImageSerialization.writeLong(inodeId, out); 2345 FSImageSerialization.writeString(path, out); 2346 FSImageSerialization.writeString(value, out); 2347 FSImageSerialization.writeLong(mtime, out); 2348 FSImageSerialization.writeLong(atime, out); 2349 permissionStatus.write(out); 2350 writeRpcIds(rpcClientId, rpcCallId, out); 2351 } 2352 2353 @Override 2354 void readFields(DataInputStream in, int logVersion) 2355 throws IOException { 2356 if (!NameNodeLayoutVersion.supports( 2357 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 2358 this.length = in.readInt(); 2359 if (this.length != 4) { 2360 throw new IOException("Incorrect data format. " 2361 + "symlink operation."); 2362 } 2363 } 2364 if (NameNodeLayoutVersion.supports( 2365 LayoutVersion.Feature.ADD_INODE_ID, logVersion)) { 2366 this.inodeId = FSImageSerialization.readLong(in); 2367 } else { 2368 // This id should be updated when the editLogOp is applied 2369 this.inodeId = INodeId.GRANDFATHER_INODE_ID; 2370 } 2371 this.path = FSImageSerialization.readString(in); 2372 this.value = FSImageSerialization.readString(in); 2373 2374 if (NameNodeLayoutVersion.supports( 2375 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 2376 this.mtime = FSImageSerialization.readLong(in); 2377 this.atime = FSImageSerialization.readLong(in); 2378 } else { 2379 this.mtime = readLong(in); 2380 this.atime = readLong(in); 2381 } 2382 this.permissionStatus = PermissionStatus.read(in); 2383 2384 // read RPC ids if necessary 2385 readRpcIds(in, logVersion); 2386 } 2387 2388 @Override 2389 public String toString() { 2390 StringBuilder builder = new StringBuilder(); 2391 builder.append("SymlinkOp [length="); 2392 builder.append(length); 2393 builder.append(", inodeId="); 2394 builder.append(inodeId); 2395 builder.append(", path="); 2396 builder.append(path); 2397 builder.append(", value="); 2398 builder.append(value); 2399 builder.append(", mtime="); 2400 builder.append(mtime); 2401 builder.append(", atime="); 2402 builder.append(atime); 2403 builder.append(", permissionStatus="); 2404 builder.append(permissionStatus); 2405 appendRpcIdsToString(builder, rpcClientId, rpcCallId); 2406 builder.append(", opCode="); 2407 builder.append(opCode); 2408 builder.append(", txid="); 2409 builder.append(txid); 2410 builder.append("]"); 2411 return builder.toString(); 2412 } 2413 2414 @Override 2415 protected void toXml(ContentHandler contentHandler) throws SAXException { 2416 XMLUtils.addSaxString(contentHandler, "LENGTH", 2417 Integer.toString(length)); 2418 XMLUtils.addSaxString(contentHandler, "INODEID", 2419 Long.toString(inodeId)); 2420 XMLUtils.addSaxString(contentHandler, "PATH", path); 2421 XMLUtils.addSaxString(contentHandler, "VALUE", value); 2422 XMLUtils.addSaxString(contentHandler, "MTIME", 2423 Long.toString(mtime)); 2424 XMLUtils.addSaxString(contentHandler, "ATIME", 2425 Long.toString(atime)); 2426 FSEditLogOp.permissionStatusToXml(contentHandler, permissionStatus); 2427 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 2428 } 2429 2430 @Override 2431 void fromXml(Stanza st) throws InvalidXmlException { 2432 this.length = Integer.parseInt(st.getValue("LENGTH")); 2433 this.inodeId = Long.parseLong(st.getValue("INODEID")); 2434 this.path = st.getValue("PATH"); 2435 this.value = st.getValue("VALUE"); 2436 this.mtime = Long.parseLong(st.getValue("MTIME")); 2437 this.atime = Long.parseLong(st.getValue("ATIME")); 2438 this.permissionStatus = permissionStatusFromXml(st); 2439 2440 readRpcIdsFromXml(st); 2441 } 2442 } 2443 2444 /** {@literal @AtMostOnce} for {@link ClientProtocol#rename2} */ 2445 static class RenameOp extends FSEditLogOp { 2446 int length; 2447 String src; 2448 String dst; 2449 long timestamp; 2450 Rename[] options; 2451 2452 private RenameOp() { 2453 super(OP_RENAME); 2454 } 2455 2456 static RenameOp getInstance(OpInstanceCache cache) { 2457 return (RenameOp)cache.get(OP_RENAME); 2458 } 2459 2460 @Override 2461 void resetSubFields() { 2462 length = 0; 2463 src = null; 2464 dst = null; 2465 timestamp = 0L; 2466 options = null; 2467 } 2468 2469 RenameOp setSource(String src) { 2470 this.src = src; 2471 return this; 2472 } 2473 2474 RenameOp setDestination(String dst) { 2475 this.dst = dst; 2476 return this; 2477 } 2478 2479 RenameOp setTimestamp(long timestamp) { 2480 this.timestamp = timestamp; 2481 return this; 2482 } 2483 2484 RenameOp setOptions(Rename[] options) { 2485 this.options = options; 2486 return this; 2487 } 2488 2489 @Override 2490 public 2491 void writeFields(DataOutputStream out) throws IOException { 2492 FSImageSerialization.writeString(src, out); 2493 FSImageSerialization.writeString(dst, out); 2494 FSImageSerialization.writeLong(timestamp, out); 2495 toBytesWritable(options).write(out); 2496 writeRpcIds(rpcClientId, rpcCallId, out); 2497 } 2498 2499 @Override 2500 void readFields(DataInputStream in, int logVersion) 2501 throws IOException { 2502 if (!NameNodeLayoutVersion.supports( 2503 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 2504 this.length = in.readInt(); 2505 if (this.length != 3) { 2506 throw new IOException("Incorrect data format. " + "Rename operation."); 2507 } 2508 } 2509 this.src = FSImageSerialization.readString(in); 2510 this.dst = FSImageSerialization.readString(in); 2511 2512 if (NameNodeLayoutVersion.supports( 2513 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 2514 this.timestamp = FSImageSerialization.readLong(in); 2515 } else { 2516 this.timestamp = readLong(in); 2517 } 2518 this.options = readRenameOptions(in); 2519 2520 // read RPC ids if necessary 2521 readRpcIds(in, logVersion); 2522 } 2523 2524 private static Rename[] readRenameOptions(DataInputStream in) throws IOException { 2525 BytesWritable writable = new BytesWritable(); 2526 writable.readFields(in); 2527 2528 byte[] bytes = writable.getBytes(); 2529 Rename[] options = new Rename[bytes.length]; 2530 2531 for (int i = 0; i < bytes.length; i++) { 2532 options[i] = Rename.valueOf(bytes[i]); 2533 } 2534 return options; 2535 } 2536 2537 static BytesWritable toBytesWritable(Rename... options) { 2538 byte[] bytes = new byte[options.length]; 2539 for (int i = 0; i < options.length; i++) { 2540 bytes[i] = options[i].value(); 2541 } 2542 return new BytesWritable(bytes); 2543 } 2544 2545 @Override 2546 public String toString() { 2547 StringBuilder builder = new StringBuilder(); 2548 builder.append("RenameOp [length="); 2549 builder.append(length); 2550 builder.append(", src="); 2551 builder.append(src); 2552 builder.append(", dst="); 2553 builder.append(dst); 2554 builder.append(", timestamp="); 2555 builder.append(timestamp); 2556 builder.append(", options="); 2557 builder.append(Arrays.toString(options)); 2558 appendRpcIdsToString(builder, rpcClientId, rpcCallId); 2559 builder.append(", opCode="); 2560 builder.append(opCode); 2561 builder.append(", txid="); 2562 builder.append(txid); 2563 builder.append("]"); 2564 return builder.toString(); 2565 } 2566 2567 @Override 2568 protected void toXml(ContentHandler contentHandler) throws SAXException { 2569 XMLUtils.addSaxString(contentHandler, "LENGTH", 2570 Integer.toString(length)); 2571 XMLUtils.addSaxString(contentHandler, "SRC", src); 2572 XMLUtils.addSaxString(contentHandler, "DST", dst); 2573 XMLUtils.addSaxString(contentHandler, "TIMESTAMP", 2574 Long.toString(timestamp)); 2575 StringBuilder bld = new StringBuilder(); 2576 String prefix = ""; 2577 for (Rename r : options) { 2578 bld.append(prefix).append(r.toString()); 2579 prefix = "|"; 2580 } 2581 XMLUtils.addSaxString(contentHandler, "OPTIONS", bld.toString()); 2582 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 2583 } 2584 2585 @Override void fromXml(Stanza st) throws InvalidXmlException { 2586 this.length = Integer.parseInt(st.getValue("LENGTH")); 2587 this.src = st.getValue("SRC"); 2588 this.dst = st.getValue("DST"); 2589 this.timestamp = Long.parseLong(st.getValue("TIMESTAMP")); 2590 String opts = st.getValue("OPTIONS"); 2591 String o[] = opts.split("\\|"); 2592 this.options = new Rename[o.length]; 2593 for (int i = 0; i < o.length; i++) { 2594 if (o[i].equals("")) 2595 continue; 2596 try { 2597 this.options[i] = Rename.valueOf(o[i]); 2598 } finally { 2599 if (this.options[i] == null) { 2600 System.err.println("error parsing Rename value: \"" + o[i] + "\""); 2601 } 2602 } 2603 } 2604 readRpcIdsFromXml(st); 2605 } 2606 } 2607 2608 /** 2609 * {@literal @Idempotent} for {@link ClientProtocol#recoverLease}. In the 2610 * meanwhile, startFile and appendFile both have their own corresponding 2611 * editlog op. 2612 */ 2613 static class ReassignLeaseOp extends FSEditLogOp { 2614 String leaseHolder; 2615 String path; 2616 String newHolder; 2617 2618 private ReassignLeaseOp() { 2619 super(OP_REASSIGN_LEASE); 2620 } 2621 2622 static ReassignLeaseOp getInstance(OpInstanceCache cache) { 2623 return (ReassignLeaseOp)cache.get(OP_REASSIGN_LEASE); 2624 } 2625 2626 @Override 2627 void resetSubFields() { 2628 leaseHolder = null; 2629 path = null; 2630 newHolder = null; 2631 } 2632 2633 ReassignLeaseOp setLeaseHolder(String leaseHolder) { 2634 this.leaseHolder = leaseHolder; 2635 return this; 2636 } 2637 2638 ReassignLeaseOp setPath(String path) { 2639 this.path = path; 2640 return this; 2641 } 2642 2643 ReassignLeaseOp setNewHolder(String newHolder) { 2644 this.newHolder = newHolder; 2645 return this; 2646 } 2647 2648 @Override 2649 public 2650 void writeFields(DataOutputStream out) throws IOException { 2651 FSImageSerialization.writeString(leaseHolder, out); 2652 FSImageSerialization.writeString(path, out); 2653 FSImageSerialization.writeString(newHolder, out); 2654 } 2655 2656 @Override 2657 void readFields(DataInputStream in, int logVersion) 2658 throws IOException { 2659 this.leaseHolder = FSImageSerialization.readString(in); 2660 this.path = FSImageSerialization.readString(in); 2661 this.newHolder = FSImageSerialization.readString(in); 2662 } 2663 2664 @Override 2665 public String toString() { 2666 StringBuilder builder = new StringBuilder(); 2667 builder.append("ReassignLeaseOp [leaseHolder="); 2668 builder.append(leaseHolder); 2669 builder.append(", path="); 2670 builder.append(path); 2671 builder.append(", newHolder="); 2672 builder.append(newHolder); 2673 builder.append(", opCode="); 2674 builder.append(opCode); 2675 builder.append(", txid="); 2676 builder.append(txid); 2677 builder.append("]"); 2678 return builder.toString(); 2679 } 2680 2681 @Override 2682 protected void toXml(ContentHandler contentHandler) throws SAXException { 2683 XMLUtils.addSaxString(contentHandler, "LEASEHOLDER", leaseHolder); 2684 XMLUtils.addSaxString(contentHandler, "PATH", path); 2685 XMLUtils.addSaxString(contentHandler, "NEWHOLDER", newHolder); 2686 } 2687 2688 @Override void fromXml(Stanza st) throws InvalidXmlException { 2689 this.leaseHolder = st.getValue("LEASEHOLDER"); 2690 this.path = st.getValue("PATH"); 2691 this.newHolder = st.getValue("NEWHOLDER"); 2692 } 2693 } 2694 2695 /** {@literal @Idempotent} for {@link ClientProtocol#getDelegationToken} */ 2696 static class GetDelegationTokenOp extends FSEditLogOp { 2697 DelegationTokenIdentifier token; 2698 long expiryTime; 2699 2700 private GetDelegationTokenOp() { 2701 super(OP_GET_DELEGATION_TOKEN); 2702 } 2703 2704 static GetDelegationTokenOp getInstance(OpInstanceCache cache) { 2705 return (GetDelegationTokenOp)cache.get(OP_GET_DELEGATION_TOKEN); 2706 } 2707 2708 @Override 2709 void resetSubFields() { 2710 token = null; 2711 expiryTime = 0L; 2712 } 2713 2714 GetDelegationTokenOp setDelegationTokenIdentifier( 2715 DelegationTokenIdentifier token) { 2716 this.token = token; 2717 return this; 2718 } 2719 2720 GetDelegationTokenOp setExpiryTime(long expiryTime) { 2721 this.expiryTime = expiryTime; 2722 return this; 2723 } 2724 2725 @Override 2726 public 2727 void writeFields(DataOutputStream out) throws IOException { 2728 token.write(out); 2729 FSImageSerialization.writeLong(expiryTime, out); 2730 } 2731 2732 @Override 2733 void readFields(DataInputStream in, int logVersion) 2734 throws IOException { 2735 this.token = new DelegationTokenIdentifier(); 2736 this.token.readFields(in); 2737 if (NameNodeLayoutVersion.supports( 2738 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 2739 this.expiryTime = FSImageSerialization.readLong(in); 2740 } else { 2741 this.expiryTime = readLong(in); 2742 } 2743 } 2744 2745 @Override 2746 public String toString() { 2747 StringBuilder builder = new StringBuilder(); 2748 builder.append("GetDelegationTokenOp [token="); 2749 builder.append(token); 2750 builder.append(", expiryTime="); 2751 builder.append(expiryTime); 2752 builder.append(", opCode="); 2753 builder.append(opCode); 2754 builder.append(", txid="); 2755 builder.append(txid); 2756 builder.append("]"); 2757 return builder.toString(); 2758 } 2759 2760 @Override 2761 protected void toXml(ContentHandler contentHandler) throws SAXException { 2762 FSEditLogOp.delegationTokenToXml(contentHandler, token); 2763 XMLUtils.addSaxString(contentHandler, "EXPIRY_TIME", 2764 Long.toString(expiryTime)); 2765 } 2766 2767 @Override void fromXml(Stanza st) throws InvalidXmlException { 2768 this.token = delegationTokenFromXml(st.getChildren( 2769 "DELEGATION_TOKEN_IDENTIFIER").get(0)); 2770 this.expiryTime = Long.parseLong(st.getValue("EXPIRY_TIME")); 2771 } 2772 } 2773 2774 /** {@literal @Idempotent} for {@link ClientProtocol#renewDelegationToken} */ 2775 static class RenewDelegationTokenOp extends FSEditLogOp { 2776 DelegationTokenIdentifier token; 2777 long expiryTime; 2778 2779 private RenewDelegationTokenOp() { 2780 super(OP_RENEW_DELEGATION_TOKEN); 2781 } 2782 2783 static RenewDelegationTokenOp getInstance(OpInstanceCache cache) { 2784 return (RenewDelegationTokenOp)cache.get(OP_RENEW_DELEGATION_TOKEN); 2785 } 2786 2787 @Override 2788 void resetSubFields() { 2789 token = null; 2790 expiryTime = 0L; 2791 } 2792 2793 RenewDelegationTokenOp setDelegationTokenIdentifier( 2794 DelegationTokenIdentifier token) { 2795 this.token = token; 2796 return this; 2797 } 2798 2799 RenewDelegationTokenOp setExpiryTime(long expiryTime) { 2800 this.expiryTime = expiryTime; 2801 return this; 2802 } 2803 2804 @Override 2805 public 2806 void writeFields(DataOutputStream out) throws IOException { 2807 token.write(out); 2808 FSImageSerialization.writeLong(expiryTime, out); 2809 } 2810 2811 @Override 2812 void readFields(DataInputStream in, int logVersion) 2813 throws IOException { 2814 this.token = new DelegationTokenIdentifier(); 2815 this.token.readFields(in); 2816 if (NameNodeLayoutVersion.supports( 2817 LayoutVersion.Feature.EDITLOG_OP_OPTIMIZATION, logVersion)) { 2818 this.expiryTime = FSImageSerialization.readLong(in); 2819 } else { 2820 this.expiryTime = readLong(in); 2821 } 2822 } 2823 2824 @Override 2825 public String toString() { 2826 StringBuilder builder = new StringBuilder(); 2827 builder.append("RenewDelegationTokenOp [token="); 2828 builder.append(token); 2829 builder.append(", expiryTime="); 2830 builder.append(expiryTime); 2831 builder.append(", opCode="); 2832 builder.append(opCode); 2833 builder.append(", txid="); 2834 builder.append(txid); 2835 builder.append("]"); 2836 return builder.toString(); 2837 } 2838 2839 @Override 2840 protected void toXml(ContentHandler contentHandler) throws SAXException { 2841 FSEditLogOp.delegationTokenToXml(contentHandler, token); 2842 XMLUtils.addSaxString(contentHandler, "EXPIRY_TIME", 2843 Long.toString(expiryTime)); 2844 } 2845 2846 @Override void fromXml(Stanza st) throws InvalidXmlException { 2847 this.token = delegationTokenFromXml(st.getChildren( 2848 "DELEGATION_TOKEN_IDENTIFIER").get(0)); 2849 this.expiryTime = Long.parseLong(st.getValue("EXPIRY_TIME")); 2850 } 2851 } 2852 2853 /** {@literal @Idempotent} for {@link ClientProtocol#cancelDelegationToken} */ 2854 static class CancelDelegationTokenOp extends FSEditLogOp { 2855 DelegationTokenIdentifier token; 2856 2857 private CancelDelegationTokenOp() { 2858 super(OP_CANCEL_DELEGATION_TOKEN); 2859 } 2860 2861 static CancelDelegationTokenOp getInstance(OpInstanceCache cache) { 2862 return (CancelDelegationTokenOp)cache.get(OP_CANCEL_DELEGATION_TOKEN); 2863 } 2864 2865 @Override 2866 void resetSubFields() { 2867 token = null; 2868 } 2869 2870 CancelDelegationTokenOp setDelegationTokenIdentifier( 2871 DelegationTokenIdentifier token) { 2872 this.token = token; 2873 return this; 2874 } 2875 2876 @Override 2877 public 2878 void writeFields(DataOutputStream out) throws IOException { 2879 token.write(out); 2880 } 2881 2882 @Override 2883 void readFields(DataInputStream in, int logVersion) 2884 throws IOException { 2885 this.token = new DelegationTokenIdentifier(); 2886 this.token.readFields(in); 2887 } 2888 2889 @Override 2890 public String toString() { 2891 StringBuilder builder = new StringBuilder(); 2892 builder.append("CancelDelegationTokenOp [token="); 2893 builder.append(token); 2894 builder.append(", opCode="); 2895 builder.append(opCode); 2896 builder.append(", txid="); 2897 builder.append(txid); 2898 builder.append("]"); 2899 return builder.toString(); 2900 } 2901 2902 @Override 2903 protected void toXml(ContentHandler contentHandler) throws SAXException { 2904 FSEditLogOp.delegationTokenToXml(contentHandler, token); 2905 } 2906 2907 @Override void fromXml(Stanza st) throws InvalidXmlException { 2908 this.token = delegationTokenFromXml(st.getChildren( 2909 "DELEGATION_TOKEN_IDENTIFIER").get(0)); 2910 } 2911 } 2912 2913 static class UpdateMasterKeyOp extends FSEditLogOp { 2914 DelegationKey key; 2915 2916 private UpdateMasterKeyOp() { 2917 super(OP_UPDATE_MASTER_KEY); 2918 } 2919 2920 static UpdateMasterKeyOp getInstance(OpInstanceCache cache) { 2921 return (UpdateMasterKeyOp)cache.get(OP_UPDATE_MASTER_KEY); 2922 } 2923 2924 @Override 2925 void resetSubFields() { 2926 key = null; 2927 } 2928 2929 UpdateMasterKeyOp setDelegationKey(DelegationKey key) { 2930 this.key = key; 2931 return this; 2932 } 2933 2934 @Override 2935 public 2936 void writeFields(DataOutputStream out) throws IOException { 2937 key.write(out); 2938 } 2939 2940 @Override 2941 void readFields(DataInputStream in, int logVersion) 2942 throws IOException { 2943 this.key = new DelegationKey(); 2944 this.key.readFields(in); 2945 } 2946 2947 @Override 2948 public String toString() { 2949 StringBuilder builder = new StringBuilder(); 2950 builder.append("UpdateMasterKeyOp [key="); 2951 builder.append(key); 2952 builder.append(", opCode="); 2953 builder.append(opCode); 2954 builder.append(", txid="); 2955 builder.append(txid); 2956 builder.append("]"); 2957 return builder.toString(); 2958 } 2959 2960 @Override 2961 protected void toXml(ContentHandler contentHandler) throws SAXException { 2962 FSEditLogOp.delegationKeyToXml(contentHandler, key); 2963 } 2964 2965 @Override void fromXml(Stanza st) throws InvalidXmlException { 2966 this.key = delegationKeyFromXml(st.getChildren( 2967 "DELEGATION_KEY").get(0)); 2968 } 2969 } 2970 2971 static class LogSegmentOp extends FSEditLogOp { 2972 private LogSegmentOp(FSEditLogOpCodes code) { 2973 super(code); 2974 assert code == OP_START_LOG_SEGMENT || 2975 code == OP_END_LOG_SEGMENT : "Bad op: " + code; 2976 } 2977 2978 static LogSegmentOp getInstance(OpInstanceCache cache, 2979 FSEditLogOpCodes code) { 2980 return (LogSegmentOp)cache.get(code); 2981 } 2982 2983 @Override 2984 void resetSubFields() { 2985 // no data stored in these ops yet 2986 } 2987 2988 @Override 2989 public void readFields(DataInputStream in, int logVersion) 2990 throws IOException { 2991 // no data stored in these ops yet 2992 } 2993 2994 @Override 2995 public 2996 void writeFields(DataOutputStream out) throws IOException { 2997 // no data stored 2998 } 2999 3000 @Override 3001 public String toString() { 3002 StringBuilder builder = new StringBuilder(); 3003 builder.append("LogSegmentOp [opCode="); 3004 builder.append(opCode); 3005 builder.append(", txid="); 3006 builder.append(txid); 3007 builder.append("]"); 3008 return builder.toString(); 3009 } 3010 3011 @Override 3012 protected void toXml(ContentHandler contentHandler) throws SAXException { 3013 // no data stored 3014 } 3015 3016 @Override void fromXml(Stanza st) throws InvalidXmlException { 3017 // do nothing 3018 } 3019 } 3020 3021 static class InvalidOp extends FSEditLogOp { 3022 private InvalidOp() { 3023 super(OP_INVALID); 3024 } 3025 3026 static InvalidOp getInstance(OpInstanceCache cache) { 3027 return (InvalidOp)cache.get(OP_INVALID); 3028 } 3029 3030 @Override 3031 void resetSubFields() { 3032 } 3033 3034 @Override 3035 public 3036 void writeFields(DataOutputStream out) throws IOException { 3037 } 3038 3039 @Override 3040 void readFields(DataInputStream in, int logVersion) 3041 throws IOException { 3042 // nothing to read 3043 } 3044 3045 @Override 3046 public String toString() { 3047 StringBuilder builder = new StringBuilder(); 3048 builder.append("InvalidOp [opCode="); 3049 builder.append(opCode); 3050 builder.append(", txid="); 3051 builder.append(txid); 3052 builder.append("]"); 3053 return builder.toString(); 3054 } 3055 @Override 3056 protected void toXml(ContentHandler contentHandler) throws SAXException { 3057 // no data stored 3058 } 3059 3060 @Override void fromXml(Stanza st) throws InvalidXmlException { 3061 // do nothing 3062 } 3063 } 3064 3065 /** 3066 * Operation corresponding to creating a snapshot. 3067 * {@literal @AtMostOnce} for {@link ClientProtocol#createSnapshot}. 3068 */ 3069 static class CreateSnapshotOp extends FSEditLogOp { 3070 String snapshotRoot; 3071 String snapshotName; 3072 3073 public CreateSnapshotOp() { 3074 super(OP_CREATE_SNAPSHOT); 3075 } 3076 3077 static CreateSnapshotOp getInstance(OpInstanceCache cache) { 3078 return (CreateSnapshotOp)cache.get(OP_CREATE_SNAPSHOT); 3079 } 3080 3081 @Override 3082 void resetSubFields() { 3083 snapshotRoot = null; 3084 snapshotName = null; 3085 } 3086 3087 CreateSnapshotOp setSnapshotName(String snapName) { 3088 this.snapshotName = snapName; 3089 return this; 3090 } 3091 3092 public CreateSnapshotOp setSnapshotRoot(String snapRoot) { 3093 snapshotRoot = snapRoot; 3094 return this; 3095 } 3096 3097 @Override 3098 void readFields(DataInputStream in, int logVersion) throws IOException { 3099 snapshotRoot = FSImageSerialization.readString(in); 3100 snapshotName = FSImageSerialization.readString(in); 3101 3102 // read RPC ids if necessary 3103 readRpcIds(in, logVersion); 3104 } 3105 3106 @Override 3107 public void writeFields(DataOutputStream out) throws IOException { 3108 FSImageSerialization.writeString(snapshotRoot, out); 3109 FSImageSerialization.writeString(snapshotName, out); 3110 writeRpcIds(rpcClientId, rpcCallId, out); 3111 } 3112 3113 @Override 3114 protected void toXml(ContentHandler contentHandler) throws SAXException { 3115 XMLUtils.addSaxString(contentHandler, "SNAPSHOTROOT", snapshotRoot); 3116 XMLUtils.addSaxString(contentHandler, "SNAPSHOTNAME", snapshotName); 3117 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 3118 } 3119 3120 @Override 3121 void fromXml(Stanza st) throws InvalidXmlException { 3122 snapshotRoot = st.getValue("SNAPSHOTROOT"); 3123 snapshotName = st.getValue("SNAPSHOTNAME"); 3124 3125 readRpcIdsFromXml(st); 3126 } 3127 3128 @Override 3129 public String toString() { 3130 StringBuilder builder = new StringBuilder(); 3131 builder.append("CreateSnapshotOp [snapshotRoot="); 3132 builder.append(snapshotRoot); 3133 builder.append(", snapshotName="); 3134 builder.append(snapshotName); 3135 appendRpcIdsToString(builder, rpcClientId, rpcCallId); 3136 builder.append("]"); 3137 return builder.toString(); 3138 } 3139 } 3140 3141 /** 3142 * Operation corresponding to delete a snapshot. 3143 * {@literal @AtMostOnce} for {@link ClientProtocol#deleteSnapshot}. 3144 */ 3145 static class DeleteSnapshotOp extends FSEditLogOp { 3146 String snapshotRoot; 3147 String snapshotName; 3148 3149 DeleteSnapshotOp() { 3150 super(OP_DELETE_SNAPSHOT); 3151 } 3152 3153 static DeleteSnapshotOp getInstance(OpInstanceCache cache) { 3154 return (DeleteSnapshotOp)cache.get(OP_DELETE_SNAPSHOT); 3155 } 3156 3157 @Override 3158 void resetSubFields() { 3159 snapshotRoot = null; 3160 snapshotName = null; 3161 } 3162 3163 DeleteSnapshotOp setSnapshotName(String snapName) { 3164 this.snapshotName = snapName; 3165 return this; 3166 } 3167 3168 DeleteSnapshotOp setSnapshotRoot(String snapRoot) { 3169 snapshotRoot = snapRoot; 3170 return this; 3171 } 3172 3173 @Override 3174 void readFields(DataInputStream in, int logVersion) throws IOException { 3175 snapshotRoot = FSImageSerialization.readString(in); 3176 snapshotName = FSImageSerialization.readString(in); 3177 3178 // read RPC ids if necessary 3179 readRpcIds(in, logVersion); 3180 } 3181 3182 @Override 3183 public void writeFields(DataOutputStream out) throws IOException { 3184 FSImageSerialization.writeString(snapshotRoot, out); 3185 FSImageSerialization.writeString(snapshotName, out); 3186 writeRpcIds(rpcClientId, rpcCallId, out); 3187 } 3188 3189 @Override 3190 protected void toXml(ContentHandler contentHandler) throws SAXException { 3191 XMLUtils.addSaxString(contentHandler, "SNAPSHOTROOT", snapshotRoot); 3192 XMLUtils.addSaxString(contentHandler, "SNAPSHOTNAME", snapshotName); 3193 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 3194 } 3195 3196 @Override 3197 void fromXml(Stanza st) throws InvalidXmlException { 3198 snapshotRoot = st.getValue("SNAPSHOTROOT"); 3199 snapshotName = st.getValue("SNAPSHOTNAME"); 3200 3201 readRpcIdsFromXml(st); 3202 } 3203 3204 @Override 3205 public String toString() { 3206 StringBuilder builder = new StringBuilder(); 3207 builder.append("DeleteSnapshotOp [snapshotRoot="); 3208 builder.append(snapshotRoot); 3209 builder.append(", snapshotName="); 3210 builder.append(snapshotName); 3211 appendRpcIdsToString(builder, rpcClientId, rpcCallId); 3212 builder.append("]"); 3213 return builder.toString(); 3214 } 3215 } 3216 3217 /** 3218 * Operation corresponding to rename a snapshot. 3219 * {@literal @AtMostOnce} for {@link ClientProtocol#renameSnapshot}. 3220 */ 3221 static class RenameSnapshotOp extends FSEditLogOp { 3222 String snapshotRoot; 3223 String snapshotOldName; 3224 String snapshotNewName; 3225 3226 RenameSnapshotOp() { 3227 super(OP_RENAME_SNAPSHOT); 3228 } 3229 3230 static RenameSnapshotOp getInstance(OpInstanceCache cache) { 3231 return (RenameSnapshotOp) cache.get(OP_RENAME_SNAPSHOT); 3232 } 3233 3234 @Override 3235 void resetSubFields() { 3236 snapshotRoot = null; 3237 snapshotOldName = null; 3238 snapshotNewName = null; 3239 } 3240 3241 RenameSnapshotOp setSnapshotOldName(String snapshotOldName) { 3242 this.snapshotOldName = snapshotOldName; 3243 return this; 3244 } 3245 3246 RenameSnapshotOp setSnapshotNewName(String snapshotNewName) { 3247 this.snapshotNewName = snapshotNewName; 3248 return this; 3249 } 3250 3251 RenameSnapshotOp setSnapshotRoot(String snapshotRoot) { 3252 this.snapshotRoot = snapshotRoot; 3253 return this; 3254 } 3255 3256 @Override 3257 void readFields(DataInputStream in, int logVersion) throws IOException { 3258 snapshotRoot = FSImageSerialization.readString(in); 3259 snapshotOldName = FSImageSerialization.readString(in); 3260 snapshotNewName = FSImageSerialization.readString(in); 3261 3262 // read RPC ids if necessary 3263 readRpcIds(in, logVersion); 3264 } 3265 3266 @Override 3267 public void writeFields(DataOutputStream out) throws IOException { 3268 FSImageSerialization.writeString(snapshotRoot, out); 3269 FSImageSerialization.writeString(snapshotOldName, out); 3270 FSImageSerialization.writeString(snapshotNewName, out); 3271 3272 writeRpcIds(rpcClientId, rpcCallId, out); 3273 } 3274 3275 @Override 3276 protected void toXml(ContentHandler contentHandler) throws SAXException { 3277 XMLUtils.addSaxString(contentHandler, "SNAPSHOTROOT", snapshotRoot); 3278 XMLUtils.addSaxString(contentHandler, "SNAPSHOTOLDNAME", snapshotOldName); 3279 XMLUtils.addSaxString(contentHandler, "SNAPSHOTNEWNAME", snapshotNewName); 3280 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 3281 } 3282 3283 @Override 3284 void fromXml(Stanza st) throws InvalidXmlException { 3285 snapshotRoot = st.getValue("SNAPSHOTROOT"); 3286 snapshotOldName = st.getValue("SNAPSHOTOLDNAME"); 3287 snapshotNewName = st.getValue("SNAPSHOTNEWNAME"); 3288 3289 readRpcIdsFromXml(st); 3290 } 3291 3292 @Override 3293 public String toString() { 3294 StringBuilder builder = new StringBuilder(); 3295 builder.append("RenameSnapshotOp [snapshotRoot="); 3296 builder.append(snapshotRoot); 3297 builder.append(", snapshotOldName="); 3298 builder.append(snapshotOldName); 3299 builder.append(", snapshotNewName="); 3300 builder.append(snapshotNewName); 3301 appendRpcIdsToString(builder, rpcClientId, rpcCallId); 3302 builder.append("]"); 3303 return builder.toString(); 3304 } 3305 } 3306 3307 /** 3308 * Operation corresponding to allow creating snapshot on a directory 3309 */ 3310 static class AllowSnapshotOp extends FSEditLogOp { // @Idempotent 3311 String snapshotRoot; 3312 3313 public AllowSnapshotOp() { 3314 super(OP_ALLOW_SNAPSHOT); 3315 } 3316 3317 public AllowSnapshotOp(String snapRoot) { 3318 super(OP_ALLOW_SNAPSHOT); 3319 snapshotRoot = snapRoot; 3320 } 3321 3322 static AllowSnapshotOp getInstance(OpInstanceCache cache) { 3323 return (AllowSnapshotOp) cache.get(OP_ALLOW_SNAPSHOT); 3324 } 3325 3326 @Override 3327 void resetSubFields() { 3328 snapshotRoot = null; 3329 } 3330 3331 public AllowSnapshotOp setSnapshotRoot(String snapRoot) { 3332 snapshotRoot = snapRoot; 3333 return this; 3334 } 3335 3336 @Override 3337 void readFields(DataInputStream in, int logVersion) throws IOException { 3338 snapshotRoot = FSImageSerialization.readString(in); 3339 } 3340 3341 @Override 3342 public void writeFields(DataOutputStream out) throws IOException { 3343 FSImageSerialization.writeString(snapshotRoot, out); 3344 } 3345 3346 @Override 3347 protected void toXml(ContentHandler contentHandler) throws SAXException { 3348 XMLUtils.addSaxString(contentHandler, "SNAPSHOTROOT", snapshotRoot); 3349 } 3350 3351 @Override 3352 void fromXml(Stanza st) throws InvalidXmlException { 3353 snapshotRoot = st.getValue("SNAPSHOTROOT"); 3354 } 3355 3356 @Override 3357 public String toString() { 3358 StringBuilder builder = new StringBuilder(); 3359 builder.append("AllowSnapshotOp [snapshotRoot="); 3360 builder.append(snapshotRoot); 3361 builder.append("]"); 3362 return builder.toString(); 3363 } 3364 } 3365 3366 /** 3367 * Operation corresponding to disallow creating snapshot on a directory 3368 */ 3369 static class DisallowSnapshotOp extends FSEditLogOp { // @Idempotent 3370 String snapshotRoot; 3371 3372 public DisallowSnapshotOp() { 3373 super(OP_DISALLOW_SNAPSHOT); 3374 } 3375 3376 public DisallowSnapshotOp(String snapRoot) { 3377 super(OP_DISALLOW_SNAPSHOT); 3378 snapshotRoot = snapRoot; 3379 } 3380 3381 static DisallowSnapshotOp getInstance(OpInstanceCache cache) { 3382 return (DisallowSnapshotOp) cache.get(OP_DISALLOW_SNAPSHOT); 3383 } 3384 3385 void resetSubFields() { 3386 snapshotRoot = null; 3387 } 3388 3389 public DisallowSnapshotOp setSnapshotRoot(String snapRoot) { 3390 snapshotRoot = snapRoot; 3391 return this; 3392 } 3393 3394 @Override 3395 void readFields(DataInputStream in, int logVersion) throws IOException { 3396 snapshotRoot = FSImageSerialization.readString(in); 3397 } 3398 3399 @Override 3400 public void writeFields(DataOutputStream out) throws IOException { 3401 FSImageSerialization.writeString(snapshotRoot, out); 3402 } 3403 3404 @Override 3405 protected void toXml(ContentHandler contentHandler) throws SAXException { 3406 XMLUtils.addSaxString(contentHandler, "SNAPSHOTROOT", snapshotRoot); 3407 } 3408 3409 @Override 3410 void fromXml(Stanza st) throws InvalidXmlException { 3411 snapshotRoot = st.getValue("SNAPSHOTROOT"); 3412 } 3413 3414 @Override 3415 public String toString() { 3416 StringBuilder builder = new StringBuilder(); 3417 builder.append("DisallowSnapshotOp [snapshotRoot="); 3418 builder.append(snapshotRoot); 3419 builder.append("]"); 3420 return builder.toString(); 3421 } 3422 } 3423 3424 /** 3425 * {@literal @AtMostOnce} for 3426 * {@link ClientProtocol#addCacheDirective} 3427 */ 3428 static class AddCacheDirectiveInfoOp extends FSEditLogOp { 3429 CacheDirectiveInfo directive; 3430 3431 public AddCacheDirectiveInfoOp() { 3432 super(OP_ADD_CACHE_DIRECTIVE); 3433 } 3434 3435 static AddCacheDirectiveInfoOp getInstance(OpInstanceCache cache) { 3436 return (AddCacheDirectiveInfoOp) cache 3437 .get(OP_ADD_CACHE_DIRECTIVE); 3438 } 3439 3440 @Override 3441 void resetSubFields() { 3442 directive = null; 3443 } 3444 3445 public AddCacheDirectiveInfoOp setDirective( 3446 CacheDirectiveInfo directive) { 3447 this.directive = directive; 3448 assert(directive.getId() != null); 3449 assert(directive.getPath() != null); 3450 assert(directive.getReplication() != null); 3451 assert(directive.getPool() != null); 3452 assert(directive.getExpiration() != null); 3453 return this; 3454 } 3455 3456 @Override 3457 void readFields(DataInputStream in, int logVersion) throws IOException { 3458 directive = FSImageSerialization.readCacheDirectiveInfo(in); 3459 readRpcIds(in, logVersion); 3460 } 3461 3462 @Override 3463 public void writeFields(DataOutputStream out) throws IOException { 3464 FSImageSerialization.writeCacheDirectiveInfo(out, directive); 3465 writeRpcIds(rpcClientId, rpcCallId, out); 3466 } 3467 3468 @Override 3469 protected void toXml(ContentHandler contentHandler) throws SAXException { 3470 FSImageSerialization.writeCacheDirectiveInfo(contentHandler, directive); 3471 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 3472 } 3473 3474 @Override 3475 void fromXml(Stanza st) throws InvalidXmlException { 3476 directive = FSImageSerialization.readCacheDirectiveInfo(st); 3477 readRpcIdsFromXml(st); 3478 } 3479 3480 @Override 3481 public String toString() { 3482 StringBuilder builder = new StringBuilder(); 3483 builder.append("AddCacheDirectiveInfo ["); 3484 builder.append("id=" + directive.getId() + ","); 3485 builder.append("path=" + directive.getPath().toUri().getPath() + ","); 3486 builder.append("replication=" + directive.getReplication() + ","); 3487 builder.append("pool=" + directive.getPool() + ","); 3488 builder.append("expiration=" + directive.getExpiration().getMillis()); 3489 appendRpcIdsToString(builder, rpcClientId, rpcCallId); 3490 builder.append("]"); 3491 return builder.toString(); 3492 } 3493 } 3494 3495 /** 3496 * {@literal @AtMostOnce} for 3497 * {@link ClientProtocol#modifyCacheDirective} 3498 */ 3499 static class ModifyCacheDirectiveInfoOp extends FSEditLogOp { 3500 CacheDirectiveInfo directive; 3501 3502 public ModifyCacheDirectiveInfoOp() { 3503 super(OP_MODIFY_CACHE_DIRECTIVE); 3504 } 3505 3506 static ModifyCacheDirectiveInfoOp getInstance(OpInstanceCache cache) { 3507 return (ModifyCacheDirectiveInfoOp) cache 3508 .get(OP_MODIFY_CACHE_DIRECTIVE); 3509 } 3510 3511 @Override 3512 void resetSubFields() { 3513 directive = null; 3514 } 3515 3516 public ModifyCacheDirectiveInfoOp setDirective( 3517 CacheDirectiveInfo directive) { 3518 this.directive = directive; 3519 assert(directive.getId() != null); 3520 return this; 3521 } 3522 3523 @Override 3524 void readFields(DataInputStream in, int logVersion) throws IOException { 3525 this.directive = FSImageSerialization.readCacheDirectiveInfo(in); 3526 readRpcIds(in, logVersion); 3527 } 3528 3529 @Override 3530 public void writeFields(DataOutputStream out) throws IOException { 3531 FSImageSerialization.writeCacheDirectiveInfo(out, directive); 3532 writeRpcIds(rpcClientId, rpcCallId, out); 3533 } 3534 3535 @Override 3536 protected void toXml(ContentHandler contentHandler) throws SAXException { 3537 FSImageSerialization.writeCacheDirectiveInfo(contentHandler, directive); 3538 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 3539 } 3540 3541 @Override 3542 void fromXml(Stanza st) throws InvalidXmlException { 3543 this.directive = FSImageSerialization.readCacheDirectiveInfo(st); 3544 readRpcIdsFromXml(st); 3545 } 3546 3547 @Override 3548 public String toString() { 3549 StringBuilder builder = new StringBuilder(); 3550 builder.append("ModifyCacheDirectiveInfoOp["); 3551 builder.append("id=").append(directive.getId()); 3552 if (directive.getPath() != null) { 3553 builder.append(",").append("path=").append(directive.getPath()); 3554 } 3555 if (directive.getReplication() != null) { 3556 builder.append(",").append("replication="). 3557 append(directive.getReplication()); 3558 } 3559 if (directive.getPool() != null) { 3560 builder.append(",").append("pool=").append(directive.getPool()); 3561 } 3562 if (directive.getExpiration() != null) { 3563 builder.append(",").append("expiration="). 3564 append(directive.getExpiration().getMillis()); 3565 } 3566 appendRpcIdsToString(builder, rpcClientId, rpcCallId); 3567 builder.append("]"); 3568 return builder.toString(); 3569 } 3570 } 3571 3572 /** 3573 * {@literal @AtMostOnce} for 3574 * {@link ClientProtocol#removeCacheDirective} 3575 */ 3576 static class RemoveCacheDirectiveInfoOp extends FSEditLogOp { 3577 long id; 3578 3579 public RemoveCacheDirectiveInfoOp() { 3580 super(OP_REMOVE_CACHE_DIRECTIVE); 3581 } 3582 3583 static RemoveCacheDirectiveInfoOp getInstance(OpInstanceCache cache) { 3584 return (RemoveCacheDirectiveInfoOp) cache 3585 .get(OP_REMOVE_CACHE_DIRECTIVE); 3586 } 3587 3588 @Override 3589 void resetSubFields() { 3590 id = 0L; 3591 } 3592 3593 public RemoveCacheDirectiveInfoOp setId(long id) { 3594 this.id = id; 3595 return this; 3596 } 3597 3598 @Override 3599 void readFields(DataInputStream in, int logVersion) throws IOException { 3600 this.id = FSImageSerialization.readLong(in); 3601 readRpcIds(in, logVersion); 3602 } 3603 3604 @Override 3605 public void writeFields(DataOutputStream out) throws IOException { 3606 FSImageSerialization.writeLong(id, out); 3607 writeRpcIds(rpcClientId, rpcCallId, out); 3608 } 3609 3610 @Override 3611 protected void toXml(ContentHandler contentHandler) throws SAXException { 3612 XMLUtils.addSaxString(contentHandler, "ID", Long.toString(id)); 3613 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 3614 } 3615 3616 @Override 3617 void fromXml(Stanza st) throws InvalidXmlException { 3618 this.id = Long.parseLong(st.getValue("ID")); 3619 readRpcIdsFromXml(st); 3620 } 3621 3622 @Override 3623 public String toString() { 3624 StringBuilder builder = new StringBuilder(); 3625 builder.append("RemoveCacheDirectiveInfo ["); 3626 builder.append("id=" + Long.toString(id)); 3627 appendRpcIdsToString(builder, rpcClientId, rpcCallId); 3628 builder.append("]"); 3629 return builder.toString(); 3630 } 3631 } 3632 3633 /** {@literal @AtMostOnce} for {@link ClientProtocol#addCachePool} */ 3634 static class AddCachePoolOp extends FSEditLogOp { 3635 CachePoolInfo info; 3636 3637 public AddCachePoolOp() { 3638 super(OP_ADD_CACHE_POOL); 3639 } 3640 3641 static AddCachePoolOp getInstance(OpInstanceCache cache) { 3642 return (AddCachePoolOp) cache.get(OP_ADD_CACHE_POOL); 3643 } 3644 3645 @Override 3646 void resetSubFields() { 3647 info = null; 3648 } 3649 3650 public AddCachePoolOp setPool(CachePoolInfo info) { 3651 this.info = info; 3652 assert(info.getPoolName() != null); 3653 assert(info.getOwnerName() != null); 3654 assert(info.getGroupName() != null); 3655 assert(info.getMode() != null); 3656 assert(info.getLimit() != null); 3657 return this; 3658 } 3659 3660 @Override 3661 void readFields(DataInputStream in, int logVersion) throws IOException { 3662 info = FSImageSerialization.readCachePoolInfo(in); 3663 readRpcIds(in, logVersion); 3664 } 3665 3666 @Override 3667 public void writeFields(DataOutputStream out) throws IOException { 3668 FSImageSerialization.writeCachePoolInfo(out, info); 3669 writeRpcIds(rpcClientId, rpcCallId, out); 3670 } 3671 3672 @Override 3673 protected void toXml(ContentHandler contentHandler) throws SAXException { 3674 FSImageSerialization.writeCachePoolInfo(contentHandler, info); 3675 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 3676 } 3677 3678 @Override 3679 void fromXml(Stanza st) throws InvalidXmlException { 3680 this.info = FSImageSerialization.readCachePoolInfo(st); 3681 readRpcIdsFromXml(st); 3682 } 3683 3684 @Override 3685 public String toString() { 3686 StringBuilder builder = new StringBuilder(); 3687 builder.append("AddCachePoolOp ["); 3688 builder.append("poolName=" + info.getPoolName() + ","); 3689 builder.append("ownerName=" + info.getOwnerName() + ","); 3690 builder.append("groupName=" + info.getGroupName() + ","); 3691 builder.append("mode=" + Short.toString(info.getMode().toShort()) + ","); 3692 builder.append("limit=" + Long.toString(info.getLimit())); 3693 appendRpcIdsToString(builder, rpcClientId, rpcCallId); 3694 builder.append("]"); 3695 return builder.toString(); 3696 } 3697 } 3698 3699 /** {@literal @AtMostOnce} for {@link ClientProtocol#modifyCachePool} */ 3700 static class ModifyCachePoolOp extends FSEditLogOp { 3701 CachePoolInfo info; 3702 3703 public ModifyCachePoolOp() { 3704 super(OP_MODIFY_CACHE_POOL); 3705 } 3706 3707 static ModifyCachePoolOp getInstance(OpInstanceCache cache) { 3708 return (ModifyCachePoolOp) cache.get(OP_MODIFY_CACHE_POOL); 3709 } 3710 3711 @Override 3712 void resetSubFields() { 3713 info = null; 3714 } 3715 3716 public ModifyCachePoolOp setInfo(CachePoolInfo info) { 3717 this.info = info; 3718 return this; 3719 } 3720 3721 @Override 3722 void readFields(DataInputStream in, int logVersion) throws IOException { 3723 info = FSImageSerialization.readCachePoolInfo(in); 3724 readRpcIds(in, logVersion); 3725 } 3726 3727 @Override 3728 public void writeFields(DataOutputStream out) throws IOException { 3729 FSImageSerialization.writeCachePoolInfo(out, info); 3730 writeRpcIds(rpcClientId, rpcCallId, out); 3731 } 3732 3733 @Override 3734 protected void toXml(ContentHandler contentHandler) throws SAXException { 3735 FSImageSerialization.writeCachePoolInfo(contentHandler, info); 3736 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 3737 } 3738 3739 @Override 3740 void fromXml(Stanza st) throws InvalidXmlException { 3741 this.info = FSImageSerialization.readCachePoolInfo(st); 3742 readRpcIdsFromXml(st); 3743 } 3744 3745 @Override 3746 public String toString() { 3747 StringBuilder builder = new StringBuilder(); 3748 builder.append("ModifyCachePoolOp ["); 3749 ArrayList<String> fields = new ArrayList<String>(5); 3750 if (info.getPoolName() != null) { 3751 fields.add("poolName=" + info.getPoolName()); 3752 } 3753 if (info.getOwnerName() != null) { 3754 fields.add("ownerName=" + info.getOwnerName()); 3755 } 3756 if (info.getGroupName() != null) { 3757 fields.add("groupName=" + info.getGroupName()); 3758 } 3759 if (info.getMode() != null) { 3760 fields.add("mode=" + info.getMode().toString()); 3761 } 3762 if (info.getLimit() != null) { 3763 fields.add("limit=" + info.getLimit()); 3764 } 3765 builder.append(Joiner.on(",").join(fields)); 3766 appendRpcIdsToString(builder, rpcClientId, rpcCallId); 3767 builder.append("]"); 3768 return builder.toString(); 3769 } 3770 } 3771 3772 /** {@literal @AtMostOnce} for {@link ClientProtocol#removeCachePool} */ 3773 static class RemoveCachePoolOp extends FSEditLogOp { 3774 String poolName; 3775 3776 public RemoveCachePoolOp() { 3777 super(OP_REMOVE_CACHE_POOL); 3778 } 3779 3780 static RemoveCachePoolOp getInstance(OpInstanceCache cache) { 3781 return (RemoveCachePoolOp) cache.get(OP_REMOVE_CACHE_POOL); 3782 } 3783 3784 @Override 3785 void resetSubFields() { 3786 poolName = null; 3787 } 3788 3789 public RemoveCachePoolOp setPoolName(String poolName) { 3790 this.poolName = poolName; 3791 return this; 3792 } 3793 3794 @Override 3795 void readFields(DataInputStream in, int logVersion) throws IOException { 3796 poolName = FSImageSerialization.readString(in); 3797 readRpcIds(in, logVersion); 3798 } 3799 3800 @Override 3801 public void writeFields(DataOutputStream out) throws IOException { 3802 FSImageSerialization.writeString(poolName, out); 3803 writeRpcIds(rpcClientId, rpcCallId, out); 3804 } 3805 3806 @Override 3807 protected void toXml(ContentHandler contentHandler) throws SAXException { 3808 XMLUtils.addSaxString(contentHandler, "POOLNAME", poolName); 3809 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 3810 } 3811 3812 @Override 3813 void fromXml(Stanza st) throws InvalidXmlException { 3814 this.poolName = st.getValue("POOLNAME"); 3815 readRpcIdsFromXml(st); 3816 } 3817 3818 @Override 3819 public String toString() { 3820 StringBuilder builder = new StringBuilder(); 3821 builder.append("RemoveCachePoolOp ["); 3822 builder.append("poolName=" + poolName); 3823 appendRpcIdsToString(builder, rpcClientId, rpcCallId); 3824 builder.append("]"); 3825 return builder.toString(); 3826 } 3827 } 3828 3829 static class RemoveXAttrOp extends FSEditLogOp { 3830 List<XAttr> xAttrs; 3831 String src; 3832 3833 private RemoveXAttrOp() { 3834 super(OP_REMOVE_XATTR); 3835 } 3836 3837 static RemoveXAttrOp getInstance() { 3838 return new RemoveXAttrOp(); 3839 } 3840 3841 @Override 3842 void resetSubFields() { 3843 xAttrs = null; 3844 src = null; 3845 } 3846 3847 @Override 3848 void readFields(DataInputStream in, int logVersion) throws IOException { 3849 XAttrEditLogProto p = XAttrEditLogProto.parseDelimitedFrom(in); 3850 src = p.getSrc(); 3851 xAttrs = PBHelper.convertXAttrs(p.getXAttrsList()); 3852 readRpcIds(in, logVersion); 3853 } 3854 3855 @Override 3856 public void writeFields(DataOutputStream out) throws IOException { 3857 XAttrEditLogProto.Builder b = XAttrEditLogProto.newBuilder(); 3858 if (src != null) { 3859 b.setSrc(src); 3860 } 3861 b.addAllXAttrs(PBHelper.convertXAttrProto(xAttrs)); 3862 b.build().writeDelimitedTo(out); 3863 // clientId and callId 3864 writeRpcIds(rpcClientId, rpcCallId, out); 3865 } 3866 3867 @Override 3868 protected void toXml(ContentHandler contentHandler) throws SAXException { 3869 XMLUtils.addSaxString(contentHandler, "SRC", src); 3870 appendXAttrsToXml(contentHandler, xAttrs); 3871 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 3872 } 3873 3874 @Override 3875 void fromXml(Stanza st) throws InvalidXmlException { 3876 src = st.getValue("SRC"); 3877 xAttrs = readXAttrsFromXml(st); 3878 readRpcIdsFromXml(st); 3879 } 3880 } 3881 3882 static class SetXAttrOp extends FSEditLogOp { 3883 List<XAttr> xAttrs; 3884 String src; 3885 3886 private SetXAttrOp() { 3887 super(OP_SET_XATTR); 3888 } 3889 3890 static SetXAttrOp getInstance() { 3891 return new SetXAttrOp(); 3892 } 3893 3894 @Override 3895 void resetSubFields() { 3896 xAttrs = null; 3897 src = null; 3898 } 3899 3900 @Override 3901 void readFields(DataInputStream in, int logVersion) throws IOException { 3902 XAttrEditLogProto p = XAttrEditLogProto.parseDelimitedFrom(in); 3903 src = p.getSrc(); 3904 xAttrs = PBHelper.convertXAttrs(p.getXAttrsList()); 3905 readRpcIds(in, logVersion); 3906 } 3907 3908 @Override 3909 public void writeFields(DataOutputStream out) throws IOException { 3910 XAttrEditLogProto.Builder b = XAttrEditLogProto.newBuilder(); 3911 if (src != null) { 3912 b.setSrc(src); 3913 } 3914 b.addAllXAttrs(PBHelper.convertXAttrProto(xAttrs)); 3915 b.build().writeDelimitedTo(out); 3916 // clientId and callId 3917 writeRpcIds(rpcClientId, rpcCallId, out); 3918 } 3919 3920 @Override 3921 protected void toXml(ContentHandler contentHandler) throws SAXException { 3922 XMLUtils.addSaxString(contentHandler, "SRC", src); 3923 appendXAttrsToXml(contentHandler, xAttrs); 3924 appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); 3925 } 3926 3927 @Override 3928 void fromXml(Stanza st) throws InvalidXmlException { 3929 src = st.getValue("SRC"); 3930 xAttrs = readXAttrsFromXml(st); 3931 readRpcIdsFromXml(st); 3932 } 3933 } 3934 3935 static class SetAclOp extends FSEditLogOp { 3936 List<AclEntry> aclEntries = Lists.newArrayList(); 3937 String src; 3938 3939 private SetAclOp() { 3940 super(OP_SET_ACL); 3941 } 3942 3943 static SetAclOp getInstance() { 3944 return new SetAclOp(); 3945 } 3946 3947 @Override 3948 void resetSubFields() { 3949 aclEntries = null; 3950 src = null; 3951 } 3952 3953 @Override 3954 void readFields(DataInputStream in, int logVersion) throws IOException { 3955 AclEditLogProto p = AclEditLogProto.parseDelimitedFrom(in); 3956 if (p == null) { 3957 throw new IOException("Failed to read fields from SetAclOp"); 3958 } 3959 src = p.getSrc(); 3960 aclEntries = PBHelper.convertAclEntry(p.getEntriesList()); 3961 } 3962 3963 @Override 3964 public void writeFields(DataOutputStream out) throws IOException { 3965 AclEditLogProto.Builder b = AclEditLogProto.newBuilder(); 3966 if (src != null) 3967 b.setSrc(src); 3968 b.addAllEntries(PBHelper.convertAclEntryProto(aclEntries)); 3969 b.build().writeDelimitedTo(out); 3970 } 3971 3972 @Override 3973 protected void toXml(ContentHandler contentHandler) throws SAXException { 3974 XMLUtils.addSaxString(contentHandler, "SRC", src); 3975 appendAclEntriesToXml(contentHandler, aclEntries); 3976 } 3977 3978 @Override 3979 void fromXml(Stanza st) throws InvalidXmlException { 3980 src = st.getValue("SRC"); 3981 aclEntries = readAclEntriesFromXml(st); 3982 if (aclEntries == null) { 3983 aclEntries = Lists.newArrayList(); 3984 } 3985 } 3986 } 3987 3988 static private short readShort(DataInputStream in) throws IOException { 3989 return Short.parseShort(FSImageSerialization.readString(in)); 3990 } 3991 3992 static private long readLong(DataInputStream in) throws IOException { 3993 return Long.parseLong(FSImageSerialization.readString(in)); 3994 } 3995 3996 /** 3997 * A class to read in blocks stored in the old format. The only two 3998 * fields in the block were blockid and length. 3999 */ 4000 static class BlockTwo implements Writable { 4001 long blkid; 4002 long len; 4003 4004 static { // register a ctor 4005 WritableFactories.setFactory 4006 (BlockTwo.class, 4007 new WritableFactory() { 4008 @Override 4009 public Writable newInstance() { return new BlockTwo(); } 4010 }); 4011 } 4012 4013 4014 BlockTwo() { 4015 blkid = 0; 4016 len = 0; 4017 } 4018 ///////////////////////////////////// 4019 // Writable 4020 ///////////////////////////////////// 4021 @Override 4022 public void write(DataOutput out) throws IOException { 4023 out.writeLong(blkid); 4024 out.writeLong(len); 4025 } 4026 4027 @Override 4028 public void readFields(DataInput in) throws IOException { 4029 this.blkid = in.readLong(); 4030 this.len = in.readLong(); 4031 } 4032 } 4033 /** 4034 * Operation corresponding to upgrade 4035 */ 4036 static class RollingUpgradeOp extends FSEditLogOp { // @Idempotent 4037 private final String name; 4038 private long time; 4039 4040 public RollingUpgradeOp(FSEditLogOpCodes code, String name) { 4041 super(code); 4042 this.name = name.toUpperCase(); 4043 } 4044 4045 static RollingUpgradeOp getStartInstance(OpInstanceCache cache) { 4046 return (RollingUpgradeOp) cache.get(OP_ROLLING_UPGRADE_START); 4047 } 4048 4049 static RollingUpgradeOp getFinalizeInstance(OpInstanceCache cache) { 4050 return (RollingUpgradeOp) cache.get(OP_ROLLING_UPGRADE_FINALIZE); 4051 } 4052 4053 @Override 4054 void resetSubFields() { 4055 time = 0L; 4056 } 4057 4058 long getTime() { 4059 return time; 4060 } 4061 4062 void setTime(long time) { 4063 this.time = time; 4064 } 4065 4066 @Override 4067 void readFields(DataInputStream in, int logVersion) throws IOException { 4068 time = in.readLong(); 4069 } 4070 4071 @Override 4072 public void writeFields(DataOutputStream out) throws IOException { 4073 FSImageSerialization.writeLong(time, out); 4074 } 4075 4076 @Override 4077 protected void toXml(ContentHandler contentHandler) throws SAXException { 4078 XMLUtils.addSaxString(contentHandler, name + "TIME", 4079 Long.toString(time)); 4080 } 4081 4082 @Override 4083 void fromXml(Stanza st) throws InvalidXmlException { 4084 this.time = Long.parseLong(st.getValue(name + "TIME")); 4085 } 4086 4087 @Override 4088 public String toString() { 4089 return new StringBuilder().append("RollingUpgradeOp [").append(name) 4090 .append(", time=").append(time).append("]").toString(); 4091 } 4092 4093 static class RollbackException extends IOException { 4094 private static final long serialVersionUID = 1L; 4095 } 4096 } 4097 4098 /** {@literal @Idempotent} for {@link ClientProtocol#setStoragePolicy} */ 4099 static class SetStoragePolicyOp extends FSEditLogOp { 4100 String path; 4101 byte policyId; 4102 4103 private SetStoragePolicyOp() { 4104 super(OP_SET_STORAGE_POLICY); 4105 } 4106 4107 static SetStoragePolicyOp getInstance(OpInstanceCache cache) { 4108 return (SetStoragePolicyOp) cache.get(OP_SET_STORAGE_POLICY); 4109 } 4110 4111 @Override 4112 void resetSubFields() { 4113 path = null; 4114 policyId = 0; 4115 } 4116 4117 SetStoragePolicyOp setPath(String path) { 4118 this.path = path; 4119 return this; 4120 } 4121 4122 SetStoragePolicyOp setPolicyId(byte policyId) { 4123 this.policyId = policyId; 4124 return this; 4125 } 4126 4127 @Override 4128 public void writeFields(DataOutputStream out) throws IOException { 4129 FSImageSerialization.writeString(path, out); 4130 out.writeByte(policyId); 4131 } 4132 4133 @Override 4134 void readFields(DataInputStream in, int logVersion) 4135 throws IOException { 4136 this.path = FSImageSerialization.readString(in); 4137 this.policyId = in.readByte(); 4138 } 4139 4140 @Override 4141 public String toString() { 4142 StringBuilder builder = new StringBuilder(); 4143 builder.append("SetStoragePolicyOp [path="); 4144 builder.append(path); 4145 builder.append(", policyId="); 4146 builder.append(policyId); 4147 builder.append(", opCode="); 4148 builder.append(opCode); 4149 builder.append(", txid="); 4150 builder.append(txid); 4151 builder.append("]"); 4152 return builder.toString(); 4153 } 4154 4155 @Override 4156 protected void toXml(ContentHandler contentHandler) throws SAXException { 4157 XMLUtils.addSaxString(contentHandler, "PATH", path); 4158 XMLUtils.addSaxString(contentHandler, "POLICYID", 4159 Byte.valueOf(policyId).toString()); 4160 } 4161 4162 @Override 4163 void fromXml(Stanza st) throws InvalidXmlException { 4164 this.path = st.getValue("PATH"); 4165 this.policyId = Byte.valueOf(st.getValue("POLICYID")); 4166 } 4167 } 4168 4169 /** 4170 * Class for writing editlog ops 4171 */ 4172 public static class Writer { 4173 private final DataOutputBuffer buf; 4174 private final Checksum checksum; 4175 4176 public Writer(DataOutputBuffer out) { 4177 this.buf = out; 4178 this.checksum = DataChecksum.newCrc32(); 4179 } 4180 4181 /** 4182 * Write an operation to the output stream 4183 * 4184 * @param op The operation to write 4185 * @throws IOException if an error occurs during writing. 4186 */ 4187 public void writeOp(FSEditLogOp op) throws IOException { 4188 int start = buf.getLength(); 4189 // write the op code first to make padding and terminator verification 4190 // work 4191 buf.writeByte(op.opCode.getOpCode()); 4192 buf.writeInt(0); // write 0 for the length first 4193 buf.writeLong(op.txid); 4194 op.writeFields(buf); 4195 int end = buf.getLength(); 4196 4197 // write the length back: content of the op + 4 bytes checksum - op_code 4198 int length = end - start - 1; 4199 buf.writeInt(length, start + 1); 4200 4201 checksum.reset(); 4202 checksum.update(buf.getData(), start, end-start); 4203 int sum = (int)checksum.getValue(); 4204 buf.writeInt(sum); 4205 } 4206 } 4207 4208 /** 4209 * Class for reading editlog ops from a stream 4210 */ 4211 public abstract static class Reader { 4212 final DataInputStream in; 4213 final StreamLimiter limiter; 4214 final OpInstanceCache cache; 4215 final byte[] temp = new byte[4096]; 4216 final int logVersion; 4217 int maxOpSize; 4218 4219 public static Reader create(DataInputStream in, StreamLimiter limiter, 4220 int logVersion) { 4221 if (logVersion < NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION) { 4222 // Use the LengthPrefixedReader on edit logs which are newer than what 4223 // we can parse. (Newer layout versions are represented by smaller 4224 // negative integers, for historical reasons.) Even though we can't 4225 // parse the Ops contained in them, we should still be able to call 4226 // scanOp on them. This is important for the JournalNode during rolling 4227 // upgrade. 4228 return new LengthPrefixedReader(in, limiter, logVersion); 4229 } else if (NameNodeLayoutVersion.supports( 4230 NameNodeLayoutVersion.Feature.EDITLOG_LENGTH, logVersion)) { 4231 return new LengthPrefixedReader(in, limiter, logVersion); 4232 } else if (NameNodeLayoutVersion.supports( 4233 LayoutVersion.Feature.EDITS_CHECKSUM, logVersion)) { 4234 Checksum checksum = DataChecksum.newCrc32(); 4235 return new ChecksummedReader(checksum, in, limiter, logVersion); 4236 } else { 4237 return new LegacyReader(in, limiter, logVersion); 4238 } 4239 } 4240 4241 /** 4242 * Construct the reader 4243 * @param in The stream to read from. 4244 * @param limiter The limiter for this stream. 4245 * @param logVersion The version of the data coming from the stream. 4246 */ 4247 Reader(DataInputStream in, StreamLimiter limiter, int logVersion) { 4248 this.in = in; 4249 this.limiter = limiter; 4250 this.logVersion = logVersion; 4251 this.cache = new OpInstanceCache(); 4252 this.maxOpSize = DFSConfigKeys.DFS_NAMENODE_MAX_OP_SIZE_DEFAULT; 4253 } 4254 4255 public void setMaxOpSize(int maxOpSize) { 4256 this.maxOpSize = maxOpSize; 4257 } 4258 4259 /** 4260 * Read an operation from the input stream. 4261 * 4262 * Note that the objects returned from this method may be re-used by future 4263 * calls to the same method. 4264 * 4265 * @param skipBrokenEdits If true, attempt to skip over damaged parts of 4266 * the input stream, rather than throwing an IOException 4267 * @return the operation read from the stream, or null at the end of the 4268 * file 4269 * @throws IOException on error. This function should only throw an 4270 * exception when skipBrokenEdits is false. 4271 */ 4272 public FSEditLogOp readOp(boolean skipBrokenEdits) throws IOException { 4273 while (true) { 4274 try { 4275 return decodeOp(); 4276 } catch (IOException e) { 4277 in.reset(); 4278 if (!skipBrokenEdits) { 4279 throw e; 4280 } 4281 } catch (RuntimeException e) { 4282 // FSEditLogOp#decodeOp is not supposed to throw RuntimeException. 4283 // However, we handle it here for recovery mode, just to be more 4284 // robust. 4285 in.reset(); 4286 if (!skipBrokenEdits) { 4287 throw e; 4288 } 4289 } catch (Throwable e) { 4290 in.reset(); 4291 if (!skipBrokenEdits) { 4292 throw new IOException("got unexpected exception " + 4293 e.getMessage(), e); 4294 } 4295 } 4296 // Move ahead one byte and re-try the decode process. 4297 if (in.skip(1) < 1) { 4298 return null; 4299 } 4300 } 4301 } 4302 4303 void verifyTerminator() throws IOException { 4304 /** The end of the edit log should contain only 0x00 or 0xff bytes. 4305 * If it contains other bytes, the log itself may be corrupt. 4306 * It is important to check this; if we don't, a stray OP_INVALID byte 4307 * could make us stop reading the edit log halfway through, and we'd never 4308 * know that we had lost data. 4309 */ 4310 limiter.clearLimit(); 4311 int numRead = -1, idx = 0; 4312 while (true) { 4313 try { 4314 numRead = -1; 4315 idx = 0; 4316 numRead = in.read(temp); 4317 if (numRead == -1) { 4318 return; 4319 } 4320 while (idx < numRead) { 4321 if ((temp[idx] != (byte)0) && (temp[idx] != (byte)-1)) { 4322 throw new IOException("Read extra bytes after " + 4323 "the terminator!"); 4324 } 4325 idx++; 4326 } 4327 } finally { 4328 // After reading each group of bytes, we reposition the mark one 4329 // byte before the next group. Similarly, if there is an error, we 4330 // want to reposition the mark one byte before the error 4331 if (numRead != -1) { 4332 in.reset(); 4333 IOUtils.skipFully(in, idx); 4334 in.mark(temp.length + 1); 4335 IOUtils.skipFully(in, 1); 4336 } 4337 } 4338 } 4339 } 4340 4341 /** 4342 * Read an opcode from the input stream. 4343 * 4344 * @return the opcode, or null on EOF. 4345 * 4346 * If an exception is thrown, the stream's mark will be set to the first 4347 * problematic byte. This usually means the beginning of the opcode. 4348 */ 4349 public abstract FSEditLogOp decodeOp() throws IOException; 4350 4351 /** 4352 * Similar to decodeOp(), but we only retrieve the transaction ID of the 4353 * Op rather than reading it. If the edit log format supports length 4354 * prefixing, this can be much faster than full decoding. 4355 * 4356 * @return the last txid of the segment, or INVALID_TXID on EOF. 4357 */ 4358 public abstract long scanOp() throws IOException; 4359 } 4360 4361 /** 4362 * Reads edit logs which are prefixed with a length. These edit logs also 4363 * include a checksum and transaction ID. 4364 */ 4365 private static class LengthPrefixedReader extends Reader { 4366 /** 4367 * The minimum length of a length-prefixed Op. 4368 * 4369 * The minimum Op has: 4370 * 1-byte opcode 4371 * 4-byte length 4372 * 8-byte txid 4373 * 0-byte body 4374 * 4-byte checksum 4375 */ 4376 private static final int MIN_OP_LENGTH = 17; 4377 4378 /** 4379 * The op id length. 4380 * 4381 * Not included in the stored length. 4382 */ 4383 private static final int OP_ID_LENGTH = 1; 4384 4385 /** 4386 * The checksum length. 4387 * 4388 * Not included in the stored length. 4389 */ 4390 private static final int CHECKSUM_LENGTH = 4; 4391 4392 private final Checksum checksum; 4393 4394 LengthPrefixedReader(DataInputStream in, StreamLimiter limiter, 4395 int logVersion) { 4396 super(in, limiter, logVersion); 4397 this.checksum = DataChecksum.newCrc32(); 4398 } 4399 4400 @Override 4401 public FSEditLogOp decodeOp() throws IOException { 4402 long txid = decodeOpFrame(); 4403 if (txid == HdfsConstants.INVALID_TXID) { 4404 return null; 4405 } 4406 in.reset(); 4407 in.mark(maxOpSize); 4408 FSEditLogOpCodes opCode = FSEditLogOpCodes.fromByte(in.readByte()); 4409 FSEditLogOp op = cache.get(opCode); 4410 if (op == null) { 4411 throw new IOException("Read invalid opcode " + opCode); 4412 } 4413 op.setTransactionId(txid); 4414 IOUtils.skipFully(in, 4 + 8); // skip length and txid 4415 op.readFields(in, logVersion); 4416 // skip over the checksum, which we validated above. 4417 IOUtils.skipFully(in, CHECKSUM_LENGTH); 4418 return op; 4419 } 4420 4421 @Override 4422 public long scanOp() throws IOException { 4423 return decodeOpFrame(); 4424 } 4425 4426 /** 4427 * Decode the opcode "frame". This includes reading the opcode and 4428 * transaction ID, and validating the checksum and length. It does not 4429 * include reading the opcode-specific fields. 4430 * The input stream will be advanced to the end of the op at the end of this 4431 * function. 4432 * 4433 * @return An op with the txid set, but none of the other fields 4434 * filled in, or null if we hit EOF. 4435 */ 4436 private long decodeOpFrame() throws IOException { 4437 limiter.setLimit(maxOpSize); 4438 in.mark(maxOpSize); 4439 byte opCodeByte; 4440 try { 4441 opCodeByte = in.readByte(); 4442 } catch (EOFException eof) { 4443 // EOF at an opcode boundary is expected. 4444 return HdfsConstants.INVALID_TXID; 4445 } 4446 if (opCodeByte == FSEditLogOpCodes.OP_INVALID.getOpCode()) { 4447 verifyTerminator(); 4448 return HdfsConstants.INVALID_TXID; 4449 } 4450 // Here, we verify that the Op size makes sense and that the 4451 // data matches its checksum before attempting to construct an Op. 4452 // This is important because otherwise we may encounter an 4453 // OutOfMemoryException which could bring down the NameNode or 4454 // JournalNode when reading garbage data. 4455 int opLength = in.readInt() + OP_ID_LENGTH + CHECKSUM_LENGTH; 4456 if (opLength > maxOpSize) { 4457 throw new IOException("Op " + (int)opCodeByte + " has size " + 4458 opLength + ", but maxOpSize = " + maxOpSize); 4459 } else if (opLength < MIN_OP_LENGTH) { 4460 throw new IOException("Op " + (int)opCodeByte + " has size " + 4461 opLength + ", but the minimum op size is " + MIN_OP_LENGTH); 4462 } 4463 long txid = in.readLong(); 4464 // Verify checksum 4465 in.reset(); 4466 in.mark(maxOpSize); 4467 checksum.reset(); 4468 for (int rem = opLength - CHECKSUM_LENGTH; rem > 0;) { 4469 int toRead = Math.min(temp.length, rem); 4470 IOUtils.readFully(in, temp, 0, toRead); 4471 checksum.update(temp, 0, toRead); 4472 rem -= toRead; 4473 } 4474 int expectedChecksum = in.readInt(); 4475 int calculatedChecksum = (int)checksum.getValue(); 4476 if (expectedChecksum != calculatedChecksum) { 4477 throw new ChecksumException( 4478 "Transaction is corrupt. Calculated checksum is " + 4479 calculatedChecksum + " but read checksum " + 4480 expectedChecksum, txid); 4481 } 4482 return txid; 4483 } 4484 } 4485 4486 /** 4487 * Read edit logs which have a checksum and a transaction ID, but not a 4488 * length. 4489 */ 4490 private static class ChecksummedReader extends Reader { 4491 private final Checksum checksum; 4492 4493 ChecksummedReader(Checksum checksum, DataInputStream in, 4494 StreamLimiter limiter, int logVersion) { 4495 super(new DataInputStream( 4496 new CheckedInputStream(in, checksum)), limiter, logVersion); 4497 this.checksum = checksum; 4498 } 4499 4500 @Override 4501 public FSEditLogOp decodeOp() throws IOException { 4502 limiter.setLimit(maxOpSize); 4503 in.mark(maxOpSize); 4504 // Reset the checksum. Since we are using a CheckedInputStream, each 4505 // subsequent read from the stream will update the checksum. 4506 checksum.reset(); 4507 byte opCodeByte; 4508 try { 4509 opCodeByte = in.readByte(); 4510 } catch (EOFException eof) { 4511 // EOF at an opcode boundary is expected. 4512 return null; 4513 } 4514 FSEditLogOpCodes opCode = FSEditLogOpCodes.fromByte(opCodeByte); 4515 if (opCode == OP_INVALID) { 4516 verifyTerminator(); 4517 return null; 4518 } 4519 FSEditLogOp op = cache.get(opCode); 4520 if (op == null) { 4521 throw new IOException("Read invalid opcode " + opCode); 4522 } 4523 op.setTransactionId(in.readLong()); 4524 op.readFields(in, logVersion); 4525 // Verify checksum 4526 int calculatedChecksum = (int)checksum.getValue(); 4527 int expectedChecksum = in.readInt(); 4528 if (expectedChecksum != calculatedChecksum) { 4529 throw new ChecksumException( 4530 "Transaction is corrupt. Calculated checksum is " + 4531 calculatedChecksum + " but read checksum " + 4532 expectedChecksum, op.txid); 4533 } 4534 return op; 4535 } 4536 4537 @Override 4538 public long scanOp() throws IOException { 4539 // Edit logs of this age don't have any length prefix, so we just have 4540 // to read the entire Op. 4541 FSEditLogOp op = decodeOp(); 4542 return op == null ? 4543 HdfsConstants.INVALID_TXID : op.getTransactionId(); 4544 } 4545 } 4546 4547 /** 4548 * Read older edit logs which may or may not have transaction IDs and other 4549 * features. This code is used during upgrades and to allow HDFS INotify to 4550 * read older edit log files. 4551 */ 4552 private static class LegacyReader extends Reader { 4553 LegacyReader(DataInputStream in, 4554 StreamLimiter limiter, int logVersion) { 4555 super(in, limiter, logVersion); 4556 } 4557 4558 @Override 4559 public FSEditLogOp decodeOp() throws IOException { 4560 limiter.setLimit(maxOpSize); 4561 in.mark(maxOpSize); 4562 byte opCodeByte; 4563 try { 4564 opCodeByte = in.readByte(); 4565 } catch (EOFException eof) { 4566 // EOF at an opcode boundary is expected. 4567 return null; 4568 } 4569 FSEditLogOpCodes opCode = FSEditLogOpCodes.fromByte(opCodeByte); 4570 if (opCode == OP_INVALID) { 4571 verifyTerminator(); 4572 return null; 4573 } 4574 FSEditLogOp op = cache.get(opCode); 4575 if (op == null) { 4576 throw new IOException("Read invalid opcode " + opCode); 4577 } 4578 if (NameNodeLayoutVersion.supports( 4579 LayoutVersion.Feature.STORED_TXIDS, logVersion)) { 4580 op.setTransactionId(in.readLong()); 4581 } else { 4582 op.setTransactionId(HdfsConstants.INVALID_TXID); 4583 } 4584 op.readFields(in, logVersion); 4585 return op; 4586 } 4587 4588 @Override 4589 public long scanOp() throws IOException { 4590 if (!NameNodeLayoutVersion.supports( 4591 LayoutVersion.Feature.STORED_TXIDS, logVersion)) { 4592 throw new IOException("Can't scan a pre-transactional edit log."); 4593 } 4594 FSEditLogOp op = decodeOp(); 4595 return op == null ? 4596 HdfsConstants.INVALID_TXID : op.getTransactionId(); 4597 } 4598 } 4599 4600 public void outputToXml(ContentHandler contentHandler) throws SAXException { 4601 contentHandler.startElement("", "", "RECORD", new AttributesImpl()); 4602 XMLUtils.addSaxString(contentHandler, "OPCODE", opCode.toString()); 4603 contentHandler.startElement("", "", "DATA", new AttributesImpl()); 4604 XMLUtils.addSaxString(contentHandler, "TXID", "" + txid); 4605 toXml(contentHandler); 4606 contentHandler.endElement("", "", "DATA"); 4607 contentHandler.endElement("", "", "RECORD"); 4608 } 4609 4610 protected abstract void toXml(ContentHandler contentHandler) 4611 throws SAXException; 4612 4613 abstract void fromXml(Stanza st) throws InvalidXmlException; 4614 4615 public void decodeXml(Stanza st) throws InvalidXmlException { 4616 this.txid = Long.parseLong(st.getValue("TXID")); 4617 fromXml(st); 4618 } 4619 4620 public static void blockToXml(ContentHandler contentHandler, Block block) 4621 throws SAXException { 4622 contentHandler.startElement("", "", "BLOCK", new AttributesImpl()); 4623 XMLUtils.addSaxString(contentHandler, "BLOCK_ID", 4624 Long.toString(block.getBlockId())); 4625 XMLUtils.addSaxString(contentHandler, "NUM_BYTES", 4626 Long.toString(block.getNumBytes())); 4627 XMLUtils.addSaxString(contentHandler, "GENSTAMP", 4628 Long.toString(block.getGenerationStamp())); 4629 contentHandler.endElement("", "", "BLOCK"); 4630 } 4631 4632 public static Block blockFromXml(Stanza st) 4633 throws InvalidXmlException { 4634 long blockId = Long.parseLong(st.getValue("BLOCK_ID")); 4635 long numBytes = Long.parseLong(st.getValue("NUM_BYTES")); 4636 long generationStamp = Long.parseLong(st.getValue("GENSTAMP")); 4637 return new Block(blockId, numBytes, generationStamp); 4638 } 4639 4640 public static void delegationTokenToXml(ContentHandler contentHandler, 4641 DelegationTokenIdentifier token) throws SAXException { 4642 contentHandler.startElement("", "", "DELEGATION_TOKEN_IDENTIFIER", new AttributesImpl()); 4643 XMLUtils.addSaxString(contentHandler, "KIND", token.getKind().toString()); 4644 XMLUtils.addSaxString(contentHandler, "SEQUENCE_NUMBER", 4645 Integer.toString(token.getSequenceNumber())); 4646 XMLUtils.addSaxString(contentHandler, "OWNER", 4647 token.getOwner().toString()); 4648 XMLUtils.addSaxString(contentHandler, "RENEWER", 4649 token.getRenewer().toString()); 4650 XMLUtils.addSaxString(contentHandler, "REALUSER", 4651 token.getRealUser().toString()); 4652 XMLUtils.addSaxString(contentHandler, "ISSUE_DATE", 4653 Long.toString(token.getIssueDate())); 4654 XMLUtils.addSaxString(contentHandler, "MAX_DATE", 4655 Long.toString(token.getMaxDate())); 4656 XMLUtils.addSaxString(contentHandler, "MASTER_KEY_ID", 4657 Integer.toString(token.getMasterKeyId())); 4658 contentHandler.endElement("", "", "DELEGATION_TOKEN_IDENTIFIER"); 4659 } 4660 4661 public static DelegationTokenIdentifier delegationTokenFromXml(Stanza st) 4662 throws InvalidXmlException { 4663 String kind = st.getValue("KIND"); 4664 if (!kind.equals(DelegationTokenIdentifier. 4665 HDFS_DELEGATION_KIND.toString())) { 4666 throw new InvalidXmlException("can't understand " + 4667 "DelegationTokenIdentifier KIND " + kind); 4668 } 4669 int seqNum = Integer.parseInt(st.getValue("SEQUENCE_NUMBER")); 4670 String owner = st.getValue("OWNER"); 4671 String renewer = st.getValue("RENEWER"); 4672 String realuser = st.getValue("REALUSER"); 4673 long issueDate = Long.parseLong(st.getValue("ISSUE_DATE")); 4674 long maxDate = Long.parseLong(st.getValue("MAX_DATE")); 4675 int masterKeyId = Integer.parseInt(st.getValue("MASTER_KEY_ID")); 4676 DelegationTokenIdentifier token = 4677 new DelegationTokenIdentifier(new Text(owner), 4678 new Text(renewer), new Text(realuser)); 4679 token.setSequenceNumber(seqNum); 4680 token.setIssueDate(issueDate); 4681 token.setMaxDate(maxDate); 4682 token.setMasterKeyId(masterKeyId); 4683 return token; 4684 } 4685 4686 public static void delegationKeyToXml(ContentHandler contentHandler, 4687 DelegationKey key) throws SAXException { 4688 contentHandler.startElement("", "", "DELEGATION_KEY", new AttributesImpl()); 4689 XMLUtils.addSaxString(contentHandler, "KEY_ID", 4690 Integer.toString(key.getKeyId())); 4691 XMLUtils.addSaxString(contentHandler, "EXPIRY_DATE", 4692 Long.toString(key.getExpiryDate())); 4693 if (key.getEncodedKey() != null) { 4694 XMLUtils.addSaxString(contentHandler, "KEY", 4695 Hex.encodeHexString(key.getEncodedKey())); 4696 } 4697 contentHandler.endElement("", "", "DELEGATION_KEY"); 4698 } 4699 4700 public static DelegationKey delegationKeyFromXml(Stanza st) 4701 throws InvalidXmlException { 4702 int keyId = Integer.parseInt(st.getValue("KEY_ID")); 4703 long expiryDate = Long.parseLong(st.getValue("EXPIRY_DATE")); 4704 byte key[] = null; 4705 try { 4706 key = Hex.decodeHex(st.getValue("KEY").toCharArray()); 4707 } catch (DecoderException e) { 4708 throw new InvalidXmlException(e.toString()); 4709 } catch (InvalidXmlException e) { 4710 } 4711 return new DelegationKey(keyId, expiryDate, key); 4712 } 4713 4714 public static void permissionStatusToXml(ContentHandler contentHandler, 4715 PermissionStatus perm) throws SAXException { 4716 contentHandler.startElement("", "", "PERMISSION_STATUS", new AttributesImpl()); 4717 XMLUtils.addSaxString(contentHandler, "USERNAME", perm.getUserName()); 4718 XMLUtils.addSaxString(contentHandler, "GROUPNAME", perm.getGroupName()); 4719 fsPermissionToXml(contentHandler, perm.getPermission()); 4720 contentHandler.endElement("", "", "PERMISSION_STATUS"); 4721 } 4722 4723 public static PermissionStatus permissionStatusFromXml(Stanza st) 4724 throws InvalidXmlException { 4725 Stanza status = st.getChildren("PERMISSION_STATUS").get(0); 4726 String username = status.getValue("USERNAME"); 4727 String groupname = status.getValue("GROUPNAME"); 4728 FsPermission mode = fsPermissionFromXml(status); 4729 return new PermissionStatus(username, groupname, mode); 4730 } 4731 4732 public static void fsPermissionToXml(ContentHandler contentHandler, 4733 FsPermission mode) throws SAXException { 4734 XMLUtils.addSaxString(contentHandler, "MODE", Short.valueOf(mode.toShort()) 4735 .toString()); 4736 } 4737 4738 public static FsPermission fsPermissionFromXml(Stanza st) 4739 throws InvalidXmlException { 4740 short mode = Short.valueOf(st.getValue("MODE")); 4741 return new FsPermission(mode); 4742 } 4743 4744 private static void fsActionToXml(ContentHandler contentHandler, FsAction v) 4745 throws SAXException { 4746 XMLUtils.addSaxString(contentHandler, "PERM", v.SYMBOL); 4747 } 4748 4749 private static FsAction fsActionFromXml(Stanza st) throws InvalidXmlException { 4750 FsAction v = FSACTION_SYMBOL_MAP.get(st.getValue("PERM")); 4751 if (v == null) 4752 throw new InvalidXmlException("Invalid value for FsAction"); 4753 return v; 4754 } 4755 4756 private static void appendAclEntriesToXml(ContentHandler contentHandler, 4757 List<AclEntry> aclEntries) throws SAXException { 4758 for (AclEntry e : aclEntries) { 4759 contentHandler.startElement("", "", "ENTRY", new AttributesImpl()); 4760 XMLUtils.addSaxString(contentHandler, "SCOPE", e.getScope().name()); 4761 XMLUtils.addSaxString(contentHandler, "TYPE", e.getType().name()); 4762 if (e.getName() != null) { 4763 XMLUtils.addSaxString(contentHandler, "NAME", e.getName()); 4764 } 4765 fsActionToXml(contentHandler, e.getPermission()); 4766 contentHandler.endElement("", "", "ENTRY"); 4767 } 4768 } 4769 4770 private static List<AclEntry> readAclEntriesFromXml(Stanza st) { 4771 List<AclEntry> aclEntries = Lists.newArrayList(); 4772 if (!st.hasChildren("ENTRY")) 4773 return null; 4774 4775 List<Stanza> stanzas = st.getChildren("ENTRY"); 4776 for (Stanza s : stanzas) { 4777 AclEntry e = new AclEntry.Builder() 4778 .setScope(AclEntryScope.valueOf(s.getValue("SCOPE"))) 4779 .setType(AclEntryType.valueOf(s.getValue("TYPE"))) 4780 .setName(s.getValueOrNull("NAME")) 4781 .setPermission(fsActionFromXml(s)).build(); 4782 aclEntries.add(e); 4783 } 4784 return aclEntries; 4785 } 4786 4787 private static void appendXAttrsToXml(ContentHandler contentHandler, 4788 List<XAttr> xAttrs) throws SAXException { 4789 for (XAttr xAttr: xAttrs) { 4790 contentHandler.startElement("", "", "XATTR", new AttributesImpl()); 4791 XMLUtils.addSaxString(contentHandler, "NAMESPACE", 4792 xAttr.getNameSpace().toString()); 4793 XMLUtils.addSaxString(contentHandler, "NAME", xAttr.getName()); 4794 if (xAttr.getValue() != null) { 4795 try { 4796 XMLUtils.addSaxString(contentHandler, "VALUE", 4797 XAttrCodec.encodeValue(xAttr.getValue(), XAttrCodec.HEX)); 4798 } catch (IOException e) { 4799 throw new SAXException(e); 4800 } 4801 } 4802 contentHandler.endElement("", "", "XATTR"); 4803 } 4804 } 4805 4806 private static List<XAttr> readXAttrsFromXml(Stanza st) 4807 throws InvalidXmlException { 4808 if (!st.hasChildren("XATTR")) { 4809 return null; 4810 } 4811 4812 List<Stanza> stanzas = st.getChildren("XATTR"); 4813 List<XAttr> xattrs = Lists.newArrayListWithCapacity(stanzas.size()); 4814 for (Stanza a: stanzas) { 4815 XAttr.Builder builder = new XAttr.Builder(); 4816 builder.setNameSpace(XAttr.NameSpace.valueOf(a.getValue("NAMESPACE"))). 4817 setName(a.getValue("NAME")); 4818 String v = a.getValueOrNull("VALUE"); 4819 if (v != null) { 4820 try { 4821 builder.setValue(XAttrCodec.decodeValue(v)); 4822 } catch (IOException e) { 4823 throw new InvalidXmlException(e.toString()); 4824 } 4825 } 4826 xattrs.add(builder.build()); 4827 } 4828 return xattrs; 4829 } 4830}