blob: 9a9d59c52529ff7da6ae9d7fc9144495f5a16fac [file] [log] [blame]
Jonathan Hartaa380972014-04-03 10:24:46 -07001package net.onrc.onos.core.intent.runtime;
Nick Karanatsios8abe7172014-02-19 20:31:48 -08002
Nick Karanatsios8abe7172014-02-19 20:31:48 -08003import java.io.ByteArrayOutputStream;
Nick Karanatsiosf9336002014-02-24 11:26:58 -08004import java.io.IOException;
Nick Karanatsiosa8800572014-02-25 01:12:06 -08005import java.nio.ByteBuffer;
6import java.util.Arrays;
Nick Karanatsios8abe7172014-02-19 20:31:48 -08007import java.util.concurrent.atomic.AtomicLong;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -07008
Jonathan Hart6df90172014-04-03 10:13:11 -07009import net.onrc.onos.core.datastore.DataStoreClient;
10import net.onrc.onos.core.datastore.IKVTable;
11import net.onrc.onos.core.datastore.ObjectExistsException;
Jonathan Hartaa380972014-04-03 10:24:46 -070012import net.onrc.onos.core.intent.IntentOperationList;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070013import net.onrc.onos.core.registry.IControllerRegistryService;
14import net.onrc.onos.core.registry.IdBlock;
Jonathan Hart23701d12014-04-03 10:45:48 -070015import net.onrc.onos.core.util.serializers.KryoFactory;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070016
Nick Karanatsiosed645df2014-02-20 23:22:29 -080017import org.slf4j.Logger;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080018import org.slf4j.LoggerFactory;
19
Jonathan Harta99ec672014-04-03 11:30:34 -070020import com.esotericsoftware.kryo.Kryo;
21import com.esotericsoftware.kryo.io.Output;
22
Nick Karanatsios8abe7172014-02-19 20:31:48 -080023/**
Nick Karanatsios8abe7172014-02-19 20:31:48 -080024 * @author nickkaranatsios
25 */
26public class PersistIntent {
Pavlin Radoslavov0c14b8a2014-05-06 16:23:48 -070027 private static final Logger log = LoggerFactory.getLogger(PersistIntent.class);
Nick Karanatsiosf9336002014-02-24 11:26:58 -080028 private long range = 10000L;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080029 private final IControllerRegistryService controllerRegistry;
Ray Milkeyec838942014-04-09 11:28:43 -070030 private static final String INTENT_JOURNAL = "G:IntentJournal";
31 private static final int VALUE_STORE_LIMIT = 1024 * 1024;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070032 private IKVTable table;
Nick Karanatsiosd1dfb922014-02-26 11:48:49 -080033 private Kryo kryo;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080034 private ByteArrayOutputStream stream;
35 private Output output = null;
36 private AtomicLong nextId = null;
37 private long rangeEnd;
38 private IdBlock idBlock = null;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070039
40
Pavlin Radoslavov0294e052014-04-10 13:36:45 -070041 public PersistIntent(final IControllerRegistryService controllerRegistry) {
Nick Karanatsios8abe7172014-02-19 20:31:48 -080042 this.controllerRegistry = controllerRegistry;
Ray Milkey5c9f2db2014-04-09 10:31:21 -070043 table = DataStoreClient.getClient().getTable(INTENT_JOURNAL);
Nick Karanatsiosed645df2014-02-20 23:22:29 -080044 stream = new ByteArrayOutputStream(1024);
Nick Karanatsios8abe7172014-02-19 20:31:48 -080045 output = new Output(stream);
Yuta HIGUCHIe8bfe612014-06-08 14:20:51 -070046 // FIXME Using KryoFactory only to register classes and not using the pool.
47 kryo = new KryoFactory(1).newKryo();
Nick Karanatsios8abe7172014-02-19 20:31:48 -080048 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070049
Nick Karanatsios8abe7172014-02-19 20:31:48 -080050 public long getKey() {
51 long key;
52 if (idBlock == null) {
53 key = getNextBlock();
54 } else {
55 key = nextId.incrementAndGet();
56 if (key >= rangeEnd) {
57 key = getNextBlock();
58 }
59 }
60 return key;
61 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070062
Nick Karanatsios8abe7172014-02-19 20:31:48 -080063 private long getNextBlock() {
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070064 // XXX This method is not thread safe, may lose allocated IdBlock
Nick Karanatsios8abe7172014-02-19 20:31:48 -080065 idBlock = controllerRegistry.allocateUniqueIdBlock(range);
66 nextId = new AtomicLong(idBlock.getStart());
67 rangeEnd = idBlock.getEnd();
68 return nextId.get();
69 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070070
Nick Karanatsios8abe7172014-02-19 20:31:48 -080071 public boolean persistIfLeader(long key, IntentOperationList operations) {
72 boolean leader = true;
73 boolean ret = false;
74 // TODO call controllerRegistry.isClusterLeader()
75 if (leader) {
76 try {
Nick Karanatsiosa8800572014-02-25 01:12:06 -080077 // reserve key 10 entries for multi-write if size over 1MB
78 key *= 10;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080079 kryo.writeObject(output, operations);
80 output.close();
Nick Karanatsiosa8800572014-02-25 01:12:06 -080081 ByteBuffer keyBytes = ByteBuffer.allocate(8).putLong(key);
Nick Karanatsios8abe7172014-02-19 20:31:48 -080082 byte[] buffer = stream.toByteArray();
Nick Karanatsiosa8800572014-02-25 01:12:06 -080083 int total = buffer.length;
Ray Milkey5c9f2db2014-04-09 10:31:21 -070084 if ((total >= VALUE_STORE_LIMIT)) {
85 int writeCount = total / VALUE_STORE_LIMIT;
86 int remainder = total % VALUE_STORE_LIMIT;
Nick Karanatsiosa8800572014-02-25 01:12:06 -080087 int upperIndex = 0;
88 for (int i = 0; i < writeCount; i++, key++) {
89 keyBytes.clear();
90 keyBytes.putLong(key);
91 keyBytes.flip();
Ray Milkey5c9f2db2014-04-09 10:31:21 -070092 upperIndex = (i * VALUE_STORE_LIMIT + VALUE_STORE_LIMIT) - 1;
93 log.debug("writing using indexes {}:{}", (i * VALUE_STORE_LIMIT), upperIndex);
94 table.create(keyBytes.array(), Arrays.copyOfRange(buffer, i * VALUE_STORE_LIMIT, upperIndex));
Nick Karanatsiosa8800572014-02-25 01:12:06 -080095 }
96 if (remainder > 0) {
97 keyBytes.clear();
98 keyBytes.putLong(key);
99 keyBytes.flip();
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 log.debug("writing using indexes {}:{}", upperIndex, total);
Nick Karanatsiosa8800572014-02-25 01:12:06 -0800101 table.create(keyBytes.array(), Arrays.copyOfRange(buffer, upperIndex + 1, total - 1));
102 }
103 } else {
104 keyBytes.flip();
105 table.create(keyBytes.array(), buffer);
106 }
Nick Karanatsiosf9336002014-02-24 11:26:58 -0800107 log.debug("key is {} value length is {}", key, buffer.length);
108 stream.reset();
109 stream.close();
Nick Karanatsiosa1bad352014-02-22 14:16:34 -0800110 log.debug("persist operations to ramcloud size of operations: {}", operations.size());
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800111 ret = true;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700112 } catch (ObjectExistsException ex) {
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800113 log.warn("Failed to store intent journal with key " + key);
Nick Karanatsiosf9336002014-02-24 11:26:58 -0800114 } catch (IOException ex) {
115 log.error("Failed to close the stream");
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800116 }
117 }
118 return ret;
119 }
120}