Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from atomix/state-machine-context
Wrap StateMachineContext and Sessions to expose ManagedResourceSession to ResourceStateMachines
- Loading branch information
Showing
6 changed files
with
208 additions
and
92 deletions.
There are no files selected for viewing
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
111 changes: 111 additions & 0 deletions
111
manager/src/main/java/io/atomix/manager/internal/ResourceManagerSessions.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,111 @@ | ||
/* | ||
* Copyright 2016 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 io.atomix.manager.internal; | ||
|
||
import io.atomix.catalyst.util.Assert; | ||
import io.atomix.copycat.server.session.ServerSession; | ||
import io.atomix.copycat.server.session.SessionListener; | ||
import io.atomix.copycat.server.session.Sessions; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* Resource manager sessions. | ||
* | ||
* @author <a href="http://github.com/kuujo>Jordan Halterman</a> | ||
*/ | ||
public class ResourceManagerSessions implements Sessions, AutoCloseable { | ||
private ManagedResourceSession first; | ||
private final Map<Long, ManagedResourceSession> sessions = new HashMap<>(); | ||
private final Set<SessionListener> listeners = new HashSet<>(); | ||
|
||
@Override | ||
public ManagedResourceSession session(long sessionId) { | ||
return sessions.get(sessionId); | ||
} | ||
|
||
void register(ManagedResourceSession session) { | ||
// If this is the first registered session, store it so it can be removed once all sessions are removed. | ||
if (first == null) | ||
first = session; | ||
|
||
// If a session was already registered for the session ID, release the new commit. | ||
if (sessions.containsKey(session.id())) { | ||
session.commit.close(); | ||
} else { | ||
sessions.put(session.id(), session); | ||
for (SessionListener listener : listeners) { | ||
listener.register(session); | ||
} | ||
} | ||
} | ||
|
||
void unregister(long sessionId) { | ||
ManagedResourceSession session = sessions.get(sessionId); | ||
if (session != null) { | ||
for (SessionListener listener : listeners) { | ||
listener.unregister(session); | ||
} | ||
} | ||
} | ||
|
||
void expire(long sessionId) { | ||
ManagedResourceSession session = sessions.get(sessionId); | ||
if (session != null) { | ||
for (SessionListener listener : listeners) { | ||
listener.expire(session); | ||
} | ||
} | ||
} | ||
|
||
void close(long sessionId) { | ||
ManagedResourceSession session = sessions.remove(sessionId); | ||
if (session != null) { | ||
for (SessionListener listener : listeners) { | ||
listener.close(session); | ||
} | ||
if (session.commit != first.commit) { | ||
session.commit.close(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Sessions addListener(SessionListener listener) { | ||
listeners.add(Assert.notNull(listener, "listener")); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Sessions removeListener(SessionListener listener) { | ||
listeners.remove(Assert.notNull(listener, "listener")); | ||
return this; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public Iterator<ServerSession> iterator() { | ||
return (Iterator) sessions.values().iterator(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
for (ManagedResourceSession session : sessions.values()) { | ||
session.commit.close(); | ||
} | ||
} | ||
|
||
} |
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
56 changes: 56 additions & 0 deletions
56
manager/src/main/java/io/atomix/manager/internal/ResourceManagerStateMachineContext.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,56 @@ | ||
/* | ||
* Copyright 2016 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 io.atomix.manager.internal; | ||
|
||
import io.atomix.copycat.server.StateMachineContext; | ||
import io.atomix.copycat.server.session.Sessions; | ||
|
||
import java.time.Clock; | ||
|
||
/** | ||
* Resource state machine context. | ||
* | ||
* @author <a href="http://github.com/kuujo>Jordan Halterman</a> | ||
*/ | ||
class ResourceManagerStateMachineContext implements StateMachineContext, AutoCloseable { | ||
private final StateMachineContext parent; | ||
final ResourceManagerSessions sessions = new ResourceManagerSessions(); | ||
|
||
ResourceManagerStateMachineContext(StateMachineContext parent) { | ||
this.parent = parent; | ||
} | ||
|
||
@Override | ||
public long index() { | ||
return parent.index(); | ||
} | ||
|
||
@Override | ||
public Clock clock() { | ||
return parent.clock(); | ||
} | ||
|
||
@Override | ||
public Sessions sessions() { | ||
return sessions; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
sessions.close(); | ||
} | ||
|
||
} |
Oops, something went wrong.