New Chronicle-Map

This commit is contained in:
leventov
2014-09-05 17:39:51 +04:00
parent 614308f38a
commit ea16356d46
95 changed files with 17623 additions and 2655 deletions
@@ -0,0 +1,173 @@
/*
* Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com
*
* 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.openhft.chronicle.map.ipc;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.model.Byteable;
/**
*
*/
public class StateMachineData implements Byteable {
private Bytes bytes;
private long offset;
/**
* c-tor
*/
public StateMachineData() {
this.bytes = null;
this.offset = -1;
}
// *************************************************************************
//
// *************************************************************************
/**
* @param states
* @return
*/
public boolean stateIn(StateMachineState... states) {
StateMachineState currentState = getState();
for (StateMachineState state : states) {
if (state == currentState) {
return true;
}
}
return false;
}
/**
* @param from
* @param to
*/
public boolean setState(StateMachineState from, StateMachineState to) {
if (this.bytes == null) throw new NullPointerException("Byteable is not set to off heap");
return this.bytes.compareAndSwapInt(this.offset, from.value(), to.value());
}
/**
* @return
*/
public StateMachineState getState() {
if (this.bytes == null) throw new NullPointerException("Byteable is not set to off heap");
int value = this.bytes.readVolatileInt(this.offset);
return StateMachineState.fromValue(value);
}
/**
* @param state
*/
public void setState(StateMachineState state) {
if (this.bytes == null) throw new NullPointerException("Byteable is not set to off heap");
this.bytes.writeInt(this.offset, state.value());
}
/**
* Wait for a state and make a transition.
* It spins initially (1000 iterations), then uses a Thread.yield() .
*
* @param from
* @param to
*/
public void waitForState(StateMachineState from, StateMachineState to) {
if (this.bytes == null) throw new NullPointerException("Byteable is not set to off heap");
// spin
for (int i = 0; !setState(from, to); i++) {
if (i > 1000) {
Thread.yield(); // back off a little.
}
}
}
// *************************************************************************
//
// *************************************************************************
/**
* @return
*/
public int getStateData() {
if (this.bytes != null) {
return this.bytes.readVolatileInt(this.offset + 4);
}
return -1;
}
/**
* @param data
*/
public void setStateData(int data) {
if (this.bytes != null) {
this.bytes.writeInt(this.offset + 4, data);
}
}
/**
* @return
*/
public int incStateData() {
if (this.bytes != null) {
return this.bytes.addInt(this.offset + 4, 1);
}
return -1;
}
/**
* @return
*/
public boolean done() {
if (this.bytes != null) {
return getStateData() > 100;
}
return true;
}
// *************************************************************************
//
// *************************************************************************
@Override
public void bytes(Bytes bytes, long offset) {
this.bytes = bytes;
this.offset = offset;
}
@Override
public Bytes bytes() {
return this.bytes;
}
@Override
public long offset() {
return this.offset;
}
@Override
public int maxSize() {
return 16;
}
}
@@ -0,0 +1,81 @@
/*
* Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com
*
* 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.openhft.chronicle.map.ipc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
*/
public class StateMachineProcessor implements Runnable {
private final StateMachineData smd;
private final StateMachineState from;
private final StateMachineState transition;
private final StateMachineState to;
private final Logger logger;
/**
* @param smd
* @param from
* @param to
*/
public StateMachineProcessor(final StateMachineData smd, StateMachineState from, StateMachineState transition, StateMachineState to) {
this.smd = smd;
this.from = from;
this.transition = transition;
this.to = to;
this.logger = LoggerFactory.getLogger(from + " => " + transition + " => " + to);
}
/**
* @param smd
* @param from
* @param transition
* @param to
*/
public static void runProcessor(final StateMachineData smd, StateMachineState from, StateMachineState transition, StateMachineState to) {
new StateMachineProcessor(smd, from, transition, to).run();
}
@Override
public void run() {
while (!smd.done()) {
if (smd.stateIn(transition)) {
doProcess();
}
logger.info("Wait for {}", from);
smd.waitForState(from, transition);
doProcess();
}
}
private void doProcess() {
smd.incStateData();
logger.info("Status {}, Next {}, Data {}",
from,
to,
smd.getStateData()
);
smd.setState(transition, to);
}
}
@@ -0,0 +1,60 @@
/*
* Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com
*
* 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.openhft.chronicle.map.ipc;
/**
*
*/
public enum StateMachineState {
UNKNOWN(-1),
STATE_0(0),
STATE_0_WORKING(1),
STATE_1(10),
STATE_1_WORKING(11),
STATE_2(20),
STATE_2_WORKING(21),
STATE_3(30),
STATE_3_WORKING(31);
private int state;
/**
* c-tor
*
* @param state
*/
StateMachineState(int state) {
this.state = state;
}
public static StateMachineState fromValue(int value) {
for (StateMachineState sms : StateMachineState.values()) {
if (sms.value() == value) {
return sms;
}
}
return StateMachineState.UNKNOWN;
}
/**
* @return
*/
public int value() {
return this.state;
}
}
@@ -0,0 +1,97 @@
/*
* Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com
*
* 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.openhft.chronicle.map.ipc;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
/**
*
*/
public class StateMachineTutorial {
private static final Logger LOGGER = LoggerFactory.getLogger(StateMachineTutorial.class);
// *************************************************************************
//
// *************************************************************************
public static void main(String[] args) throws Exception {
ChronicleMap<Integer, StateMachineData> map = null;
try {
File dataFile = new File(System.getProperty("java.io.tmpdir"), "hft-state-machine");
map = ChronicleMapBuilder.of(Integer.class, StateMachineData.class)
.entries(8)
.create(dataFile);
if (args.length > 0) {
if ("0".equalsIgnoreCase(args[0])) {
StateMachineData smd =
map.acquireUsing(0, new StateMachineData());
StateMachineState st = smd.getState();
if (st == StateMachineState.STATE_0) {
long start = System.nanoTime();
//fire the first state change
smd.setStateData(0);
smd.setState(StateMachineState.STATE_0, StateMachineState.STATE_1);
while (!smd.done()) {
// busy wait
}
long end = System.nanoTime();
LOGGER.info("Took {} us for 100 transiction", (end - start) / 1E3);
}
} else if ("1".equalsIgnoreCase(args[0])) {
StateMachineProcessor.runProcessor(
map.acquireUsing(0, new StateMachineData()),
StateMachineState.STATE_1,
StateMachineState.STATE_1_WORKING,
StateMachineState.STATE_2);
} else if ("2".equalsIgnoreCase(args[0])) {
StateMachineProcessor.runProcessor(
map.acquireUsing(0, new StateMachineData()),
StateMachineState.STATE_2,
StateMachineState.STATE_2_WORKING,
StateMachineState.STATE_3);
} else if ("3".equalsIgnoreCase(args[0])) {
StateMachineProcessor.runProcessor(
map.acquireUsing(0, new StateMachineData()),
StateMachineState.STATE_3,
StateMachineState.STATE_3_WORKING,
StateMachineState.STATE_1);
} else if ("clean".equalsIgnoreCase(args[0])) {
LOGGER.info("deleting {}", dataFile.getAbsolutePath());
dataFile.delete();
}
}
} finally {
if (map != null) {
map.close();
}
}
}
}