Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Commit to facilitate cleaning entries via the Commit API.
- Loading branch information
Showing
12 changed files
with
460 additions
and
271 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
core/src/main/java/net/kuujo/copycat/manager/ResourceCommit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* Licensed 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. | ||
*/ | ||
package net.kuujo.copycat.manager; | ||
|
||
import net.kuujo.copycat.raft.Operation; | ||
import net.kuujo.copycat.raft.Session; | ||
import net.kuujo.copycat.raft.server.Commit; | ||
|
||
/** | ||
* Resource commit. | ||
* | ||
* @author <a href="http://github.com/kuujo">Jordan Halterman</a> | ||
*/ | ||
class ResourceCommit implements Commit { | ||
private final ResourceCommitPool pool; | ||
private Commit commit; | ||
private Session session; | ||
|
||
public ResourceCommit(ResourceCommitPool pool) { | ||
this.pool = pool; | ||
} | ||
|
||
/** | ||
* Resets the resource commit. | ||
* | ||
* @param commit The parent commit. | ||
* @param session The resource session. | ||
*/ | ||
void reset(Commit commit, Session session) { | ||
this.commit = commit; | ||
this.session = session; | ||
} | ||
|
||
@Override | ||
public long index() { | ||
return commit.index(); | ||
} | ||
|
||
@Override | ||
public Session session() { | ||
return session; | ||
} | ||
|
||
@Override | ||
public long timestamp() { | ||
return commit.timestamp(); | ||
} | ||
|
||
@Override | ||
public Class type() { | ||
return commit.type(); | ||
} | ||
|
||
@Override | ||
public Operation operation() { | ||
return commit.operation(); | ||
} | ||
|
||
@Override | ||
public void clean() { | ||
commit.clean(); | ||
pool.release(this); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%s[index=%d, session=%s, timestamp=%d, operation=%s]", getClass().getSimpleName(), index(), session(), timestamp(), operation()); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
core/src/main/java/net/kuujo/copycat/manager/ResourceCommitPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* Licensed 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. | ||
*/ | ||
package net.kuujo.copycat.manager; | ||
|
||
import net.kuujo.copycat.raft.Session; | ||
import net.kuujo.copycat.raft.server.Commit; | ||
|
||
import java.util.Queue; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
|
||
/** | ||
* Resource commit pool. | ||
* | ||
* @author <a href="http://github.com/kuujo">Jordan Halterman</a> | ||
*/ | ||
class ResourceCommitPool { | ||
private final Queue<ResourceCommit> pool = new ConcurrentLinkedQueue<>(); | ||
|
||
/** | ||
* Acquires a commit from the pool. | ||
* | ||
* @param commit The commit to acquire. | ||
* @param session The resource session. | ||
* @return The acquired resource commit. | ||
*/ | ||
public ResourceCommit acquire(Commit commit, Session session) { | ||
ResourceCommit resourceCommit = pool.poll(); | ||
if (resourceCommit == null) { | ||
resourceCommit = new ResourceCommit(this); | ||
resourceCommit.reset(commit, session); | ||
} | ||
return resourceCommit; | ||
} | ||
|
||
/** | ||
* Releases a commit to the pool. | ||
* | ||
* @param commit The commit to release. | ||
*/ | ||
public void release(ResourceCommit commit) { | ||
pool.add(commit); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
server/src/main/java/net/kuujo/copycat/raft/server/state/ServerCommit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* Licensed 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. | ||
*/ | ||
package net.kuujo.copycat.raft.server.state; | ||
|
||
import net.kuujo.copycat.raft.Operation; | ||
import net.kuujo.copycat.raft.Session; | ||
import net.kuujo.copycat.raft.server.Commit; | ||
import net.kuujo.copycat.raft.server.storage.OperationEntry; | ||
|
||
/** | ||
* Server commit. | ||
* | ||
* @author <a href="http://github.com/kuujo">Jordan Halterman</a> | ||
*/ | ||
class ServerCommit implements Commit { | ||
private final ServerCommitPool pool; | ||
private final ServerCommitCleaner cleaner; | ||
private final SessionManager sessions; | ||
private OperationEntry entry; | ||
private Session session; | ||
|
||
public ServerCommit(ServerCommitPool pool, ServerCommitCleaner cleaner, SessionManager sessions) { | ||
this.pool = pool; | ||
this.cleaner = cleaner; | ||
this.sessions = sessions; | ||
} | ||
|
||
/** | ||
* Resets the commit. | ||
* | ||
* @param entry The entry. | ||
*/ | ||
void reset(OperationEntry entry) { | ||
this.entry = entry; | ||
this.session = sessions.getSession(entry.getSession()); | ||
} | ||
|
||
@Override | ||
public long index() { | ||
return entry.getIndex(); | ||
} | ||
|
||
@Override | ||
public Session session() { | ||
return session; | ||
} | ||
|
||
@Override | ||
public long timestamp() { | ||
return entry.getTimestamp(); | ||
} | ||
|
||
@Override | ||
public Class type() { | ||
return entry.getOperation().getClass(); | ||
} | ||
|
||
@Override | ||
public Operation operation() { | ||
return entry.getOperation(); | ||
} | ||
|
||
@Override | ||
public void clean() { | ||
cleaner.clean(entry); | ||
pool.release(this); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%s[index=%d, session=%s, timestamp=%d, operation=%s]", getClass().getSimpleName(), index(), session(), timestamp(), operation()); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
server/src/main/java/net/kuujo/copycat/raft/server/state/ServerCommitCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* Licensed 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. | ||
*/ | ||
package net.kuujo.copycat.raft.server.state; | ||
|
||
import net.kuujo.copycat.io.storage.Entry; | ||
import net.kuujo.copycat.io.storage.Log; | ||
|
||
/** | ||
* Server commit cleaner. | ||
* | ||
* @author <a href="http://github.com/kuujo">Jordan Halterman</a> | ||
*/ | ||
class ServerCommitCleaner { | ||
private final Log log; | ||
|
||
ServerCommitCleaner(Log log) { | ||
this.log = log; | ||
} | ||
|
||
/** | ||
* Cleans the given index from the log. | ||
* | ||
* @param entry The entry to clean. | ||
*/ | ||
void clean(Entry entry) { | ||
log.cleanEntry(entry.getIndex()); | ||
entry.close(); | ||
} | ||
|
||
} |
Oops, something went wrong.