Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache healing. Closes #30. #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
63 changes: 54 additions & 9 deletions src/net/peacefulcraft/borough/storage/BoroughClaimStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public BoroughClaim createClaim(String claimName, UUID owner) {
* @return Claim meta object or NULL of no such claim exists
*/
public BoroughClaim getClaim(String name) throws IllegalArgumentException {
// check cache
BoroughClaim claim = null;
synchronized(this.claimCache) {
claim = this.claimCache.get(name);
Expand All @@ -145,6 +146,7 @@ public BoroughClaim getClaim(String name) throws IllegalArgumentException {
}

if (claim == null) {
// check SQL
claim = SQLQueries.getBoroughClaim(nameParts[1], owner);
synchronized(this.claimCache) {
// Other threads may have been fetching while we were.
Expand All @@ -156,9 +158,35 @@ public BoroughClaim getClaim(String name) throws IllegalArgumentException {
return winner;
}
}
} else {
synchronized(this.claimCache) {
this.claimCache.put(getClaimKey(nameParts[0], nameParts[1]), claim);
}

return claim;
}

/**
* Get the BuroughClaim meta object by the given claim name. Performs blocking SQL work.
* @param name Claim name
* @param forceCanonical Force SQL query, bypassing memory cache
* @return Claim meta object or NULL of no such claim exists
*/
public BoroughClaim getClaim(String name, boolean forceCanonical) throws IllegalArgumentException {
if (!forceCanonical) { return this.getClaim(name); }

String[] nameParts = splitClaimKey(name);
UUID owner = Borough.getUUIDCache().usernameToUUID(nameParts[0]);
if (owner == null) {
throw new IllegalArgumentException("No known user " + nameParts[0] + ".");
}

BoroughClaim claim = SQLQueries.getBoroughClaim(nameParts[1], owner);
synchronized(this.claimCache) {
// Other threads may have been fetching while we were.
// If they beat us, yeild and use their objects.
BoroughClaim winner = this.claimCache.get(getClaimKey(nameParts[0], nameParts[1]));
if (winner == null) {
claimCache.put(getClaimKey(nameParts[0], nameParts[1]), claim);
} else {
return winner;
}
}

Expand Down Expand Up @@ -251,13 +279,30 @@ public BoroughChunk getChunk(String world, int x, int z) {
synchronized(this.chunkCache) {
chunk = this.chunkCache.get(getChunkKey(world, x, z));
}

// Check DB

if (chunk == null) {
chunk = SQLQueries.getBoroughChunk(world, x, z);
synchronized(this.chunkCache) {
this.chunkCache.put(getChunkKey(world, x, z), chunk);
}
return this.getChunk(world, x, z, true);
} else {
return null;
}
}

/**
* Get claim information about the requested chunk. Performs blocking SQL work.
*
* @param world World chunk is in
* @param x Chunk x coordinate. (Not world coordinates)
* @param z Chunk z coordinate. (Not world coordinates)
* @param forceCanonical Skip memory cache and only trust SQL store.
* @return BoroughChunk object.
*/
public BoroughChunk getChunk(String world, int x, int z, boolean forceCanonical) {
if (!forceCanonical) { return this.getChunk(world, x, z); }

// Check DB
BoroughChunk chunk = SQLQueries.getBoroughChunk(world, x, z);
synchronized(this.chunkCache) {
this.chunkCache.put(getChunkKey(world, x, z), chunk);
}

// Doesn't exist. Return wrapper with null claim meta.
Expand Down
8 changes: 8 additions & 0 deletions src/net/peacefulcraft/borough/storage/SQLQueries.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public static BoroughClaim createClaim(String name, UUID owner) {

} catch (SQLException ex) {
Borough._this().logSevere("Error creating claim " + name + " for " + owner + ". ");
// duplicate key, try to refresh cache because it is probably wrong if this happens.
if (ex.getErrorCode() == 1022) {
Borough.mysqlThreadPool.execute(() -> { Borough.getClaimStore().getClaim(name, true); });
}
throw new RuntimeException("Query error.", ex);
}
}
Expand Down Expand Up @@ -162,6 +166,10 @@ public static void claimChunk(BoroughClaim claimSource, BoroughChunk claimTarget

} catch (SQLException ex) {
Borough._this().logSevere("Error extending claim " + claimSource.getClaimId() + " (" + claimTarget.getWorld() + "," + claimTarget.getChunkX() + "," + claimTarget.getChunkZ() + ").");
// duplicate key, try to refresh cache because it is probably wrong if this happens.
if (ex.getErrorCode() == 1022) {
Borough.mysqlThreadPool.execute(() -> { Borough.getClaimStore().getChunk(claimTarget.getWorld(), claimTarget.getChunkX(), claimTarget.getChunkZ(), true); });
}
throw new RuntimeException("Query error.", ex);
}
}
Expand Down