Ejemplo n.º 1
0
 /**
  * serialize the snapshot
  *
  * @param oa the output archive to which the snapshot needs to be serialized
  * @throws IOException
  * @throws InterruptedException
  */
 public void serializeSnapshot(OutputArchive oa) throws IOException, InterruptedException {
   SerializeUtils.serializeSnapshot(getDataTree(), oa, getSessionWithTimeOuts());
 }
 /**
  * play the log from this logstream into the datatree
  *
  * @param logStream
  * @return
  * @throws IOException
  */
 public long playLog(InputArchive logStream) throws IOException {
   long highestZxid = 0;
   try {
     while (true) {
       byte[] bytes = logStream.readBuffer("txnEntry");
       if (bytes.length == 0) {
         // Since we preallocate, we define EOF to be an
         // empty transaction
         throw new EOFException();
       }
       InputArchive ia = BinaryInputArchive.getArchive(new ByteArrayInputStream(bytes));
       TxnHeader hdr = new TxnHeader();
       Record txn = SerializeUtils.deserializeTxn(ia, hdr);
       if (logStream.readByte("EOR") != 'B') {
         LOG.warn("Last transaction was partial.");
         throw new EOFException("Last transaction was partial.");
       }
       if (hdr.getZxid() <= highestZxid && highestZxid != 0) {
         LOG.error(
             highestZxid
                 + "(higestZxid) >= "
                 + hdr.getZxid()
                 + "(next log) for type "
                 + hdr.getType());
       } else {
         highestZxid = hdr.getZxid();
       }
       switch (hdr.getType()) {
         case OpCode.createSession:
           sessionsWithTimeouts.put(hdr.getClientId(), ((CreateSessionTxn) txn).getTimeOut());
           if (LOG.isTraceEnabled()) {
             ZooTrace.logTraceMessage(
                 LOG,
                 ZooTrace.SESSION_TRACE_MASK,
                 "playLog --- create session in log: 0x"
                     + Long.toHexString(hdr.getClientId())
                     + " with timeout: "
                     + ((CreateSessionTxn) txn).getTimeOut());
           }
           // give dataTree a chance to sync its lastProcessedZxid
           oldDataTree.processTxn(hdr, txn);
           break;
         case OpCode.closeSession:
           sessionsWithTimeouts.remove(hdr.getClientId());
           if (LOG.isTraceEnabled()) {
             ZooTrace.logTraceMessage(
                 LOG,
                 ZooTrace.SESSION_TRACE_MASK,
                 "playLog --- close session in log: 0x" + Long.toHexString(hdr.getClientId()));
           }
           oldDataTree.processTxn(hdr, txn);
           break;
         default:
           oldDataTree.processTxn(hdr, txn);
       }
       Request r = new Request(null, 0, hdr.getCxid(), hdr.getType(), null, null);
       r.txn = txn;
       r.hdr = hdr;
       r.zxid = hdr.getZxid();
     }
   } catch (EOFException e) {
     // expected in some cases - see comments in try block
   }
   return highestZxid;
 }
Ejemplo n.º 3
0
 /**
  * deserialize a snapshot from an input archive
  *
  * @param ia the input archive you want to deserialize from
  * @throws IOException
  */
 public void deserializeSnapshot(InputArchive ia) throws IOException {
   clear();
   SerializeUtils.deserializeSnapshot(getDataTree(), ia, getSessionWithTimeOuts());
   initialized = true;
 }
  public static String packetToString(QuorumPacket p) {
    if (true) return null;
    String type = null;
    String mess = null;
    Record txn = null;

    switch (p.getType()) {
      case Leader.ACK:
        type = "ACK";
        break;
      case Leader.COMMIT:
        type = "COMMIT";
        break;
      case Leader.FOLLOWERINFO:
        type = "FOLLOWERINFO";
        break;
      case Leader.NEWLEADER:
        type = "NEWLEADER";
        break;
      case Leader.PING:
        type = "PING";
        break;
      case Leader.PROPOSAL:
        type = "PROPOSAL";
        BinaryInputArchive ia =
            BinaryInputArchive.getArchive(new ByteArrayInputStream(p.getData()));
        TxnHeader hdr = new TxnHeader();
        try {
          txn = SerializeUtils.deserializeTxn(ia, hdr);
          // mess = "transaction: " + txn.toString();
        } catch (IOException e) {
          LOG.warn("Unexpected exception", e);
        }
        break;
      case Leader.REQUEST:
        type = "REQUEST";
        break;
      case Leader.REVALIDATE:
        type = "REVALIDATE";
        ByteArrayInputStream bis = new ByteArrayInputStream(p.getData());
        DataInputStream dis = new DataInputStream(bis);
        try {
          long id = dis.readLong();
          mess = " sessionid = " + id;
        } catch (IOException e) {
          LOG.warn("Unexpected exception", e);
        }

        break;
      case Leader.UPTODATE:
        type = "UPTODATE";
        break;
      default:
        type = "UNKNOWN" + p.getType();
    }
    String entry = null;
    if (type != null) {
      entry = type + " " + Long.toHexString(p.getZxid()) + " " + mess;
    }
    return entry;
  }