Skip to content

Commit

Permalink
Merge pull request #4 from maismail/fix_memcache
Browse files Browse the repository at this point in the history
Move setPartitionKey switch from Lock to BaseINodeLock
  • Loading branch information
maismail committed May 22, 2015
2 parents d30b91a + 2ddc769 commit 019ad49
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 30 deletions.
125 changes: 104 additions & 21 deletions src/main/java/io/hops/transaction/context/TransactionsStats.java
Expand Up @@ -103,17 +103,46 @@ public String toString(){
}
}


public static class ResolvingCacheStat {
public static enum Op{
GET,
SET
}
private Op operation;
private long elapsed;
private int roundTrips;

public ResolvingCacheStat(Op operation, long elapsed, int roundTrips){
this.operation = operation;
this.elapsed = elapsed;
this.roundTrips = roundTrips;
}

static String getHeader(){
return "Operation, Elapsed, RoundTrips";
}
@Override
public String toString() {
return operation.toString() +"," + elapsed + "," + roundTrips;
}
}

private boolean enabled;
private int WRITER_ROUND;
private BufferedWriter statsLogWriter;
private BufferedWriter csvFileWriter;
private BufferedWriter memcacheCSVFileWriter;
private List<TransactionStat> transactionStats;
private File statsDir;
private Thread writerThread;
private List<ResolvingCacheStat> resolvingCacheStats;
private boolean detailedStats;

private TransactionsStats() {
this.enabled = false;
this.transactionStats = new LinkedList<TransactionStat>();
this.resolvingCacheStats = new LinkedList<ResolvingCacheStat>();
}

public static TransactionsStats getInstance() {
Expand All @@ -125,7 +154,7 @@ public static TransactionsStats getInstance() {


public void setConfiguration(boolean enableOrDisable, String statsDir, int
writerRound)
writerRound, boolean detailed)
throws IOException {
if (enableOrDisable) {
this.enabled = true;
Expand Down Expand Up @@ -154,6 +183,7 @@ public void run() {
}
});
this.writerThread.start();
this.detailedStats = detailed;
} else {
this.enabled = false;
BaseEntityContext.disableStats();
Expand All @@ -177,12 +207,29 @@ public TransactionStat collectStats(RequestHandler.OperationType operationType,
return null;
}

public void pushResolvingCacheStats(ResolvingCacheStat stat){
if(enabled){
synchronized (resolvingCacheStats){
resolvingCacheStats.add(stat);
}
}
}

private void dump() throws IOException {
if (enabled) {
synchronized (transactionStats) {
dumpOrdered();
dumpCSVLike();
clear();
if(!transactionStats.isEmpty()) {
dumpDetailed();
dumpCSVLike();
clear();
}
}
synchronized (resolvingCacheStats){
if(!resolvingCacheStats.isEmpty()) {
dumpResolvingCacheStats();
memcacheCSVFileWriter.flush();
resolvingCacheStats.clear();
}
}
}
}
Expand All @@ -191,21 +238,48 @@ public void close() throws IOException {
if(enabled) {
enabled = false;
writerThread.interrupt();
statsLogWriter.close();
csvFileWriter.close();
if(statsLogWriter != null) {
statsLogWriter.close();
}
if(csvFileWriter != null) {
csvFileWriter.close();
}
if(memcacheCSVFileWriter != null){
memcacheCSVFileWriter.close();
}
}
}

public boolean isEnabled(){
return enabled;
}

private void clear() throws IOException {
statsLogWriter.flush();
csvFileWriter.flush();
if(statsLogWriter != null){
statsLogWriter.flush();
}
if(csvFileWriter != null) {
csvFileWriter.flush();
}
transactionStats.clear();
}

private void dumpResolvingCacheStats() throws IOException{
boolean fileExists = getResolvingCacheCSVFile().exists();
BufferedWriter writer = getResolvingCSVFileWriter();
if(!fileExists) {
writer.write(ResolvingCacheStat.getHeader());
writer.newLine();
}
for(ResolvingCacheStat stat : resolvingCacheStats){
writer.write(stat.toString());
writer.newLine();
}
}

private void dumpCSVLike() throws IOException{
boolean fileExists = getCSVFile().exists();
BufferedWriter writer = getCSVFileWriter();
BufferedWriter writer = getCSVFileWriter();
if(!fileExists) {
writer.write(TransactionStat.getHeader());
writer.newLine();
Expand All @@ -216,21 +290,19 @@ private void dumpCSVLike() throws IOException{
}
}

private void dumpOrdered() throws IOException {
for (TransactionStat stat : transactionStats) {
dumpOrdered(stat);
private void dumpDetailed() throws IOException {
if(detailedStats) {
BufferedWriter writer = getStatsLogWriter();
for (TransactionStat stat : transactionStats) {
writer.write("Transaction: " + stat.name.toString());
writer.newLine();
dump(writer, stat);
writer.newLine();
writer.newLine();
}
}
}

private void dumpOrdered(TransactionStat transactionStat) throws IOException {
BufferedWriter writer = getStatsLogWriter();
writer.write("Transaction: " + transactionStat.name.toString());
writer.newLine();
dump(writer, transactionStat);
writer.newLine();
writer.newLine();
}

private EntityContextStat.StatsAggregator dump(BufferedWriter writer,
TransactionStat transactionStat) throws IOException {

Expand Down Expand Up @@ -269,6 +341,14 @@ private BufferedWriter getStatsLogWriter() throws IOException {
return this.statsLogWriter;
}

private BufferedWriter getResolvingCSVFileWriter() throws IOException {
if (memcacheCSVFileWriter == null) {
this.memcacheCSVFileWriter = new BufferedWriter(
new FileWriter(getResolvingCacheCSVFile(), true));
}
return this.memcacheCSVFileWriter;
}

private File getStatsFile() {
return new File(statsDir, "hops-stats.log");
}
Expand All @@ -277,4 +357,7 @@ private File getCSVFile() {
return new File(statsDir, "hops-stats.csv");
}

private File getResolvingCacheCSVFile() {
return new File(statsDir, "hops-resolving-cache-stats.csv");
}
}
9 changes: 0 additions & 9 deletions src/main/java/io/hops/transaction/lock/Lock.java
Expand Up @@ -39,7 +39,6 @@
import java.util.Collection;

public abstract class Lock implements Comparable<Lock> {
private static boolean setPartitionKeyEnabled = false;

/*
* The Order of entries in Type defines the order
Expand Down Expand Up @@ -119,12 +118,4 @@ protected <T> T acquireLock(TransactionLockTypes.LockType lock,
}
return EntityManager.find(finder, param);
}

static void enableSetPartitionKey(boolean enable) {
setPartitionKeyEnabled = enable;
}

static boolean isSetPartitionKeyEnabled() {
return setPartitionKeyEnabled;
}
}

0 comments on commit 019ad49

Please sign in to comment.