Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/main/java/io/github/protocol/bookkeeper/BookieInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.protocol.bookkeeper;

public class BookieInfo {
private long freeSpace;

private long totalSpace;

public long getFreeSpace() {
return freeSpace;
}

public void setFreeSpace(long freeSpace) {
this.freeSpace = freeSpace;
}

public long getTotalSpace() {
return totalSpace;
}

public void setTotalSpace(long totalSpace) {
this.totalSpace = totalSpace;
}
}
43 changes: 43 additions & 0 deletions src/main/java/io/github/protocol/bookkeeper/BookieStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.github.protocol.bookkeeper;

public class BookieStatus {
private boolean running;

private boolean readOnly;

private boolean shuttingDown;

private boolean availableForHighPriorityWrites;

public boolean isRunning() {
return running;
}

public void setRunning(boolean running) {
this.running = running;
}

public boolean isReadOnly() {
return readOnly;
}

public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
}

public boolean isShuttingDown() {
return shuttingDown;
}

public void setShuttingDown(boolean shuttingDown) {
this.shuttingDown = shuttingDown;
}

public boolean isAvailableForHighPriorityWrites() {
return availableForHighPriorityWrites;
}

public void setAvailableForHighPriorityWrites(boolean availableForHighPriorityWrites) {
this.availableForHighPriorityWrites = availableForHighPriorityWrites;
}
}
40 changes: 40 additions & 0 deletions src/main/java/io/github/protocol/bookkeeper/Bookies.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.github.protocol.bookkeeper;

import java.util.List;
import java.util.Map;

public interface Bookies {

Map<String, String> bookieList() throws BookkeeperAdminException;

Map<String, String> listBookieInfo() throws BookkeeperAdminException;

LastLogMark lastLogMark() throws BookkeeperAdminException;

DiskFile listDiskFile() throws BookkeeperAdminException;

void expandStorage() throws BookkeeperAdminException;

void forceGc(boolean forceMajor, boolean forceMinor) throws BookkeeperAdminException;

boolean isInForceGc() throws BookkeeperAdminException;

void suspendGc(boolean major, boolean minor) throws BookkeeperAdminException;

GcSuspendStatus gcSuspendStatus() throws BookkeeperAdminException;

void resumeGc(boolean major, boolean minor) throws BookkeeperAdminException;

List<GarbageCollectionStatus> gcStatusList() throws BookkeeperAdminException;

BookieStatus status() throws BookkeeperAdminException;

void setReadOnly(boolean readOnly) throws BookkeeperAdminException;

boolean isReadOnly() throws BookkeeperAdminException;

boolean isReady() throws BookkeeperAdminException;

BookieInfo bookieInfo() throws BookkeeperAdminException;

}
228 changes: 228 additions & 0 deletions src/main/java/io/github/protocol/bookkeeper/BookiesImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
package io.github.protocol.bookkeeper;

import com.fasterxml.jackson.core.type.TypeReference;

import java.io.IOException;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class BookiesImpl implements Bookies {

private final InnerHttpClient innerHttpClient;

public BookiesImpl(InnerHttpClient innerHttpClient) {
this.innerHttpClient = innerHttpClient;
}

@Override
public Map<String, String> bookieList() throws BookkeeperAdminException {
try {
HttpResponse<String> resp = innerHttpClient.get(UrlConst.BOOKIE_LIST);
return JacksonService.toRefer(resp.body(), new TypeReference<Map<String, String>>() {
});
} catch (Exception e) {
throw new BookkeeperAdminException(e);
}
}

@Override
public Map<String, String> listBookieInfo() throws BookkeeperAdminException {
try {
HttpResponse<String> resp = innerHttpClient.get(UrlConst.BOOKIE_LIST_INFO);
return JacksonService.toRefer(resp.body(), new TypeReference<Map<String, String>>() {
});
} catch (Exception e) {
throw new BookkeeperAdminException(e);
}

}

@Override
public LastLogMark lastLogMark() throws BookkeeperAdminException {
Map<String, String> map;
try {
HttpResponse<String> resp = innerHttpClient.get(UrlConst.BOOKIE_LAST_LOG_MARK);
map = JacksonService.toRefer(resp.body(), new TypeReference<Map<String, String>>() {
});
} catch (Exception e) {
throw new BookkeeperAdminException(e);
}
LastLogMark lastLogMark = new LastLogMark();
HashMap<Integer, Integer> lastLogMarks = new HashMap<>();
for (Map.Entry<String, String> entry : map.entrySet()) {
lastLogMarks.put(lastLogMark.extractValueFromLastLogMarkKey(entry.getKey()),
lastLogMark.extractValueFromLastLogMarkValue(entry.getValue()));
}
lastLogMark.setLogFileIdTxnMap(lastLogMarks);
return lastLogMark;
}

@Override
public DiskFile listDiskFile() throws BookkeeperAdminException {
Map<String, String> map;
try {
HttpResponse<String> resp = innerHttpClient.get(UrlConst.BOOKIE_LIST_DISK_FILE);
map = JacksonService.toRefer(resp.body(), new TypeReference<Map<String, String>>() {
});
} catch (Exception e) {
throw new BookkeeperAdminException(e);
}

DiskFile diskFile = new DiskFile();
diskFile.setIndexFiles(map.get("index files").split("\t"));
diskFile.setJournalFiles(map.get("journal files").split("\t"));
diskFile.setEntryLogFiles(map.get("entrylog files").split("\t"));
return diskFile;
}

@Override
public void expandStorage() throws BookkeeperAdminException {
try {
innerHttpClient.put(UrlConst.BOOKIE_EXPAND_STORAGE);
} catch (IOException | InterruptedException e) {
throw new BookkeeperAdminException(e);
}
}

@Override
public void forceGc(boolean forceMajor, boolean forceMinor) throws BookkeeperAdminException {
HashMap<String, Boolean> requestBody = new HashMap<>();
requestBody.put("forceMajor", forceMajor);
requestBody.put("forceMinor", forceMinor);
try {
innerHttpClient.put(UrlConst.BOOKIE_GC, JacksonService.toJson(requestBody));
} catch (IOException | InterruptedException e) {
throw new BookkeeperAdminException(e);
}
}

@Override
public boolean isInForceGc() throws BookkeeperAdminException {
Map<String, String> map;
try {
HttpResponse<String> resp = innerHttpClient.get(UrlConst.BOOKIE_GC);
map = JacksonService.toRefer(resp.body(), new TypeReference<Map<String, String>>() {
});
} catch (Exception e) {
throw new BookkeeperAdminException(e);
}
return Boolean.parseBoolean(map.get("is_in_force_gc"));
}

@Override
public void suspendGc(boolean major, boolean minor) throws BookkeeperAdminException {
HashMap<String, Boolean> requestBody = new HashMap<>();
requestBody.put("suspendMajor", major);
requestBody.put("suspendMinor", minor);
try {
innerHttpClient.put(UrlConst.BOOKIE_GC_SUSPEND_COMPACTION, JacksonService.toJson(requestBody));
} catch (IOException | InterruptedException e) {
throw new BookkeeperAdminException(e);
}
}

@Override
public GcSuspendStatus gcSuspendStatus() throws BookkeeperAdminException {
try {
HttpResponse<String> resp = innerHttpClient.get(UrlConst.BOOKIE_GC_SUSPEND_COMPACTION);
Map<String, Boolean> map = JacksonService.toRefer(resp.body(), new TypeReference<Map<String, Boolean>>() {
});
boolean isMajorGcSuspended = map.get("isMajorGcSuspended");
boolean isMinorGcSuspended = map.get("isMinorGcSuspended");
return new GcSuspendStatus(isMajorGcSuspended, isMinorGcSuspended);
} catch (Exception e) {
throw new BookkeeperAdminException(e);
}
}

@Override
public void resumeGc(boolean major, boolean minor) throws BookkeeperAdminException {
HashMap<String, Boolean> requestBody = new HashMap<>();
requestBody.put("resumeMajor", major);
requestBody.put("resumeMinor", minor);
try {
innerHttpClient.put(UrlConst.BOOKIE_GC_RESUME_COMPACTION, JacksonService.toJson(requestBody));
} catch (IOException | InterruptedException e) {
throw new BookkeeperAdminException(e);
}
}

@Override
public List<GarbageCollectionStatus> gcStatusList() throws BookkeeperAdminException {
List<Map<String, Object>> list;
try {
HttpResponse<String> resp = innerHttpClient.get(UrlConst.BOOKIE_GC_DETAILS);
list = JacksonService.toRefer(resp.body(), new TypeReference<List<Map<String, Object>>>() {
});
} catch (Exception e) {
throw new BookkeeperAdminException(e);
}
return list.stream().map(map -> {
GarbageCollectionStatus garbageCollectionStatus = new GarbageCollectionStatus();
garbageCollectionStatus.setForceCompacting((boolean) map.get("forceCompacting"));
garbageCollectionStatus.setMajorCompacting((boolean) map.get("majorCompacting"));
garbageCollectionStatus.setMinorCompacting((boolean) map.get("minorCompacting"));
garbageCollectionStatus.setLastMajorCompactionTime((long) map.get("lastMajorCompactionTime"));
garbageCollectionStatus.setLastMinorCompactionTime((long) map.get("lastMinorCompactionTime"));
garbageCollectionStatus.setMajorCompactionCounter((int) map.get("majorCompactionCounter"));
garbageCollectionStatus.setMinorCompactionCounter((int) map.get("minorCompactionCounter"));
return garbageCollectionStatus;
}).collect(Collectors.toList());
}

@Override
public BookieStatus status() throws BookkeeperAdminException {
try {
HttpResponse<String> resp = innerHttpClient.get(UrlConst.BOOKIE_STATE);
return JacksonService.toObject(resp.body(), BookieStatus.class);
} catch (Exception e) {
throw new BookkeeperAdminException(e);
}
}

@Override
public void setReadOnly(boolean readOnly) throws BookkeeperAdminException {
HashMap<String, Boolean> requestBody = new HashMap<>();
requestBody.put("readOnly", readOnly);
try {
innerHttpClient.put(UrlConst.BOOKIE_STATE_READ_ONLY, JacksonService.toJson(requestBody));
} catch (IOException | InterruptedException e) {
throw new BookkeeperAdminException(e);
}
}

@Override
public boolean isReadOnly() throws BookkeeperAdminException {
try {
HttpResponse<String> resp = innerHttpClient.get(UrlConst.BOOKIE_STATE_READ_ONLY);
Map<String, Boolean> map = JacksonService.toRefer(resp.body(), new TypeReference<Map<String, Boolean>>() {
});
return map.get("readOnly");
} catch (Exception e) {
throw new BookkeeperAdminException(e);
}
}

@Override
public boolean isReady() throws BookkeeperAdminException {
try {
HttpResponse<String> resp = innerHttpClient.get(UrlConst.BOOKIE_READY);
return "OK".equals(resp.body());
} catch (Exception e) {
throw new BookkeeperAdminException(e);
}
}

@Override
public BookieInfo bookieInfo() throws BookkeeperAdminException {
try {
HttpResponse<String> resp = innerHttpClient.get(UrlConst.BOOKIE_INFO);
return JacksonService.toObject(resp.body(), BookieInfo.class);
} catch (Exception e) {
throw new BookkeeperAdminException(e);
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/io/github/protocol/bookkeeper/DiskFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.github.protocol.bookkeeper;

public class DiskFile {
private String[] indexFiles;
private String[] journalFiles;
private String[] entryLogFiles;

public String[] getIndexFiles() {
return indexFiles;
}

public void setIndexFiles(String[] indexFiles) {
this.indexFiles = indexFiles;
}

public String[] getJournalFiles() {
return journalFiles;
}

public void setJournalFiles(String[] journalFiles) {
this.journalFiles = journalFiles;
}

public String[] getEntryLogFiles() {
return entryLogFiles;
}

public void setEntryLogFiles(String[] entryLogFiles) {
this.entryLogFiles = entryLogFiles;
}
}
Loading