Quantcast
Channel: Recent Gists from ivanyu
Viewing all articles
Browse latest Browse all 5

ZooKeeper transaction log decoder

$
0
0
decode.py
importcodecs
importsys
forlineinsys.stdin:
line=line.rstrip()
hash_i=line.find("#")
print(line[:hash_i-1])
s1=line[hash_i+1:]
comma_i=s1.find(",")
s1=s1[:comma_i]
payload=codecs.decode(s1, "hex")
print(payload)
print()
LogFormatter.java
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
packageorg.apache.zookeeper.server;
importjava.io.ByteArrayInputStream;
importjava.io.EOFException;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.time.ZoneId;
importjava.time.ZonedDateTime;
importjava.time.format.DateTimeFormatter;
importjava.time.format.DateTimeFormatterBuilder;
importjava.util.Calendar;
importjava.util.Date;
importjava.util.GregorianCalendar;
importjava.util.List;
importjava.util.TimeZone;
importjava.util.zip.Adler32;
importjava.util.zip.Checksum;
importorg.apache.jute.BinaryInputArchive;
importorg.apache.jute.InputArchive;
importorg.apache.jute.Record;
importorg.apache.yetus.audience.InterfaceAudience;
importorg.apache.zookeeper.KeeperException;
importorg.apache.zookeeper.ZooDefs;
importorg.apache.zookeeper.server.persistence.FileHeader;
importorg.apache.zookeeper.server.persistence.FileTxnLog;
importorg.apache.zookeeper.server.util.SerializeUtils;
importorg.apache.zookeeper.txn.CheckVersionTxn;
importorg.apache.zookeeper.txn.CreateTxn;
importorg.apache.zookeeper.txn.DeleteTxn;
importorg.apache.zookeeper.txn.ErrorTxn;
importorg.apache.zookeeper.txn.MultiTxn;
importorg.apache.zookeeper.txn.SetDataTxn;
importorg.apache.zookeeper.txn.Txn;
importorg.apache.zookeeper.txn.TxnHeader;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importstaticjava.time.format.DateTimeFormatter.ISO_LOCAL_TIME;
@InterfaceAudience.Public
publicclassLogFormatter {
privatestaticfinalLoggerLOG = LoggerFactory.getLogger(LogFormatter.class);
/**
* @param args
*/
publicstaticvoidmain(String[] args) throwsException {
if (args.length != 1) {
System.err.println("USAGE: LogFormatter log_file");
System.exit(2);
}
FileInputStreamfis = newFileInputStream(args[0]);
BinaryInputArchivelogStream = BinaryInputArchive.getArchive(fis);
FileHeaderfhdr = newFileHeader();
fhdr.deserialize(logStream, "fileheader");
if (fhdr.getMagic() != FileTxnLog.TXNLOG_MAGIC) {
System.err.println("Invalid magic number for " + args[0]);
System.exit(2);
}
// System.out.println("ZooKeeper Transactional Log File with dbid "
// + fhdr.getDbid() + " txnlog format version "
// + fhdr.getVersion());
intcount = 0;
while (true) {
longcrcValue;
byte[] bytes;
try {
crcValue = logStream.readLong("crcvalue");
bytes = logStream.readBuffer("txnEntry");
} catch (EOFExceptione) {
System.out.println("EOF reached after " + count + " txns.");
return;
}
if (bytes.length == 0) {
// Since we preallocate, we define EOF to be an
// empty transaction
System.out.println("EOF reached after " + count + " txns.");
return;
}
Checksumcrc = newAdler32();
crc.update(bytes, 0, bytes.length);
if (crcValue != crc.getValue()) {
thrownewIOException("CRC doesn't match " + crcValue +
" vs " + crc.getValue());
}
TxnHeaderhdr = newTxnHeader();
Recordtxn = SerializeUtils.deserializeTxn(bytes, hdr);
finalCalendarc = newGregorianCalendar(TimeZone.getTimeZone("UTC"));
c.set(2020, Calendar.JANUARY, 27, 14, 20);
finalDatelimit = c.getTime();
if (newDate(hdr.getTime()).after(limit)) {
break;
}
DateTimeFormatterformatter = newDateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(ISO_LOCAL_TIME)
.toFormatter();
finalStringline = formatter.format(ZonedDateTime.ofInstant(newDate(hdr.getTime()).toInstant(), ZoneId.of("UTC")))
+ "\tsession 0x"
+ Long.toHexString(hdr.getClientId())
+ "(" + Long.toString(hdr.getClientId()) + ")"
+ "\tcxid 0x"
// + Long.toHexString(hdr.getCxid())
// + "\tzxid 0x"
+ Long.toHexString(hdr.getZxid())
+ " " + TraceFormatter.op2String(hdr.getType()) + " ";
if (shouldDisplayTransaction(hdr, txn)) {
if (txn == null) {
System.out.println(line);
} else {
System.out.print(line);
System.out.println(format(hdr.getType(), txn, ""));
System.out.println();
}
}
if (logStream.readByte("EOR") != 'B') {
LOG.error("Last transaction was partial.");
thrownewEOFException("Last transaction was partial.");
}
count++;
}
}
privatestaticbooleanshouldDisplayTransaction(TxnHeaderhdr, Recordtxn) {
if (hdr.getType() == ZooDefs.OpCode.createSession
|| hdr.getType() == ZooDefs.OpCode.closeSession
|| hdr.getType() == ZooDefs.OpCode.error) {
returnfalse;
}
if (hdr.getType() == ZooDefs.OpCode.setData
&& (
((SetDataTxn) txn).getPath().startsWith("/prune/kafka_topic_partition_sizes")
|| ((SetDataTxn) txn).getPath().startsWith("/prune/mm_sources")
|| ((SetDataTxn) txn).getPath().startsWith("/prune/disk_usage")
|| ((SetDataTxn) txn).getPath().startsWith("/prune/kafka_restart_lock")
|| ((SetDataTxn) txn).getPath().startsWith("/prune/active_kafka_cluster_node_ids")
|| ((SetDataTxn) txn).getPath().startsWith("/prune/karuka"))) {
returnfalse;
}
if (hdr.getType() == ZooDefs.OpCode.delete
&& (
((DeleteTxn) txn).getPath().startsWith("/prune/kafka_topic_partition_sizes")
|| ((DeleteTxn) txn).getPath().startsWith("/prune/mm_sources")
|| ((DeleteTxn) txn).getPath().startsWith("/prune/disk_usage")
|| ((DeleteTxn) txn).getPath().startsWith("/prune/kafka_restart_lock")
|| ((DeleteTxn) txn).getPath().startsWith("/prune/active_kafka_cluster_node_ids")
|| ((DeleteTxn) txn).getPath().startsWith("/prune/karuka"))) {
returnfalse;
}
if (hdr.getType() == ZooDefs.OpCode.create
&& (
((CreateTxn) txn).getPath().startsWith("/prune/kafka_topic_partition_sizes")
|| ((CreateTxn) txn).getPath().startsWith("/prune/mm_sources")
|| ((CreateTxn) txn).getPath().startsWith("/prune/disk_usage")
|| ((CreateTxn) txn).getPath().startsWith("/prune/kafka_restart_lock")
|| ((CreateTxn) txn).getPath().startsWith("/prune/active_kafka_cluster_node_ids")
|| ((CreateTxn) txn).getPath().startsWith("/prune/karuka"))) {
returnfalse;
}
returntrue;
}
privatestaticStringformat(inttype, Recordtxn, Stringprefix) throwsIOException {
if (type == ZooDefs.OpCode.create) {
CreateTxncreateTxn = (CreateTxn) txn;
Stringline = createTxn.getPath()
+ " " + (createTxn.getEphemeral() ? "E" : "nE")
;
if (createTxn.getData() != null) {
line += " " + newString(createTxn.getData());
}
returnprefix + line;
}
if (type == ZooDefs.OpCode.setData) {
SetDataTxnsetDataTxn = (SetDataTxn) txn;
Stringline = setDataTxn.getPath() + " " + setDataTxn.getVersion();
if (setDataTxn.getData() != null) {
line += " " + newString(setDataTxn.getData());
}
returnprefix + line;
}
if (type == ZooDefs.OpCode.check) {
CheckVersionTxncheckVersionTxn = (CheckVersionTxn) txn;
Stringdata = checkVersionTxn.getPath() + " " + checkVersionTxn.getVersion();
returnprefix + data;
}
if (type == ZooDefs.OpCode.error) {
ErrorTxnerrorTxn = (ErrorTxn) txn;
finalKeeperException.CodeerrorCode = KeeperException.Code.get(errorTxn.getErr());
finalKeeperExceptionkeeperException = KeeperException.create(errorCode);
Stringdata = "" + errorCode;
returnprefix + data;
}
if (type == ZooDefs.OpCode.multi) {
Stringresult = "\n";
MultiTxnmultiTxn = (MultiTxn) txn;
finalList<Txn> innerTxns = multiTxn.getTxns();
for (inti = 0; i< innerTxns.size(); i++) {
finalTxninnerTxn = innerTxns.get(i);
Recordrecord = null;
switch (innerTxn.getType()) {
caseZooDefs.OpCode.create:
record = newCreateTxn();
break;
caseZooDefs.OpCode.delete:
record = newDeleteTxn();
break;
caseZooDefs.OpCode.setData:
record = newSetDataTxn();
break;
caseZooDefs.OpCode.check:
record = newCheckVersionTxn();
break;
caseZooDefs.OpCode.error:
record = newErrorTxn();
break;
default:
thrownewRuntimeException("Type: " + innerTxn.getType());
}
finalByteArrayInputStreambais = newByteArrayInputStream(innerTxn.getData());
InputArchiveia = BinaryInputArchive.getArchive(bais);
record.deserialize(ia, "txn");
result += "\t" + prefix + TraceFormatter.op2String(innerTxn.getType()) + " ";
result += format(innerTxn.getType(), record, "");
if (i< innerTxns.size() - 1) {
result += "\n";
}
}
returnresult;
}
return"";
}
}

Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images