blob: e2b2917fc37824b45afc5b160210badbc42cda5f [file] [log] [blame]
Nick Karanatsios8abe7172014-02-19 20:31:48 -08001/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
Jonathan Hartaa380972014-04-03 10:24:46 -07005package net.onrc.onos.core.intent.runtime;
Nick Karanatsios8abe7172014-02-19 20:31:48 -08006
Nick Karanatsios8abe7172014-02-19 20:31:48 -08007import java.io.ByteArrayOutputStream;
Nick Karanatsiosf9336002014-02-24 11:26:58 -08008import java.io.IOException;
Nick Karanatsiosa8800572014-02-25 01:12:06 -08009import java.nio.ByteBuffer;
10import java.util.Arrays;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080011import java.util.concurrent.atomic.AtomicLong;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070012
Jonathan Hart6df90172014-04-03 10:13:11 -070013import net.onrc.onos.core.datagrid.web.IntentResource;
14import net.onrc.onos.core.datastore.DataStoreClient;
15import net.onrc.onos.core.datastore.IKVTable;
16import net.onrc.onos.core.datastore.ObjectExistsException;
Jonathan Hartaa380972014-04-03 10:24:46 -070017import net.onrc.onos.core.intent.IntentOperationList;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070018import net.onrc.onos.core.registry.IControllerRegistryService;
19import net.onrc.onos.core.registry.IdBlock;
Jonathan Hart23701d12014-04-03 10:45:48 -070020import net.onrc.onos.core.util.serializers.KryoFactory;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070021
Nick Karanatsiosed645df2014-02-20 23:22:29 -080022import org.slf4j.Logger;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080023import org.slf4j.LoggerFactory;
24
Jonathan Harta99ec672014-04-03 11:30:34 -070025import com.esotericsoftware.kryo.Kryo;
26import com.esotericsoftware.kryo.io.Output;
27
Nick Karanatsios8abe7172014-02-19 20:31:48 -080028/**
Nick Karanatsios8abe7172014-02-19 20:31:48 -080029 * @author nickkaranatsios
30 */
31public class PersistIntent {
Ray Milkeyec838942014-04-09 11:28:43 -070032 private static final Logger log = LoggerFactory.getLogger(IntentResource.class);
Nick Karanatsiosf9336002014-02-24 11:26:58 -080033 private long range = 10000L;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080034 private final IControllerRegistryService controllerRegistry;
Ray Milkeyec838942014-04-09 11:28:43 -070035 private static final String INTENT_JOURNAL = "G:IntentJournal";
36 private static final int VALUE_STORE_LIMIT = 1024 * 1024;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070037 private IKVTable table;
Nick Karanatsiosd1dfb922014-02-26 11:48:49 -080038 private Kryo kryo;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080039 private ByteArrayOutputStream stream;
40 private Output output = null;
41 private AtomicLong nextId = null;
42 private long rangeEnd;
43 private IdBlock idBlock = null;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070044
45
Pavlin Radoslavov0294e052014-04-10 13:36:45 -070046 public PersistIntent(final IControllerRegistryService controllerRegistry) {
Nick Karanatsios8abe7172014-02-19 20:31:48 -080047 this.controllerRegistry = controllerRegistry;
Ray Milkey5c9f2db2014-04-09 10:31:21 -070048 table = DataStoreClient.getClient().getTable(INTENT_JOURNAL);
Nick Karanatsiosed645df2014-02-20 23:22:29 -080049 stream = new ByteArrayOutputStream(1024);
Nick Karanatsios8abe7172014-02-19 20:31:48 -080050 output = new Output(stream);
Nick Karanatsiosd1dfb922014-02-26 11:48:49 -080051 kryo = (new KryoFactory()).newKryo();
Nick Karanatsios8abe7172014-02-19 20:31:48 -080052 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070053
Nick Karanatsios8abe7172014-02-19 20:31:48 -080054 public long getKey() {
55 long key;
56 if (idBlock == null) {
57 key = getNextBlock();
58 } else {
59 key = nextId.incrementAndGet();
60 if (key >= rangeEnd) {
61 key = getNextBlock();
62 }
63 }
64 return key;
65 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070066
Nick Karanatsios8abe7172014-02-19 20:31:48 -080067 private long getNextBlock() {
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070068 // XXX This method is not thread safe, may lose allocated IdBlock
Nick Karanatsios8abe7172014-02-19 20:31:48 -080069 idBlock = controllerRegistry.allocateUniqueIdBlock(range);
70 nextId = new AtomicLong(idBlock.getStart());
71 rangeEnd = idBlock.getEnd();
72 return nextId.get();
73 }
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070074
Nick Karanatsios8abe7172014-02-19 20:31:48 -080075 public boolean persistIfLeader(long key, IntentOperationList operations) {
76 boolean leader = true;
77 boolean ret = false;
78 // TODO call controllerRegistry.isClusterLeader()
79 if (leader) {
80 try {
Nick Karanatsiosa8800572014-02-25 01:12:06 -080081 // reserve key 10 entries for multi-write if size over 1MB
82 key *= 10;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080083 kryo.writeObject(output, operations);
84 output.close();
Nick Karanatsiosa8800572014-02-25 01:12:06 -080085 ByteBuffer keyBytes = ByteBuffer.allocate(8).putLong(key);
Nick Karanatsios8abe7172014-02-19 20:31:48 -080086 byte[] buffer = stream.toByteArray();
Nick Karanatsiosa8800572014-02-25 01:12:06 -080087 int total = buffer.length;
Ray Milkey5c9f2db2014-04-09 10:31:21 -070088 if ((total >= VALUE_STORE_LIMIT)) {
89 int writeCount = total / VALUE_STORE_LIMIT;
90 int remainder = total % VALUE_STORE_LIMIT;
Nick Karanatsiosa8800572014-02-25 01:12:06 -080091 int upperIndex = 0;
92 for (int i = 0; i < writeCount; i++, key++) {
93 keyBytes.clear();
94 keyBytes.putLong(key);
95 keyBytes.flip();
Ray Milkey5c9f2db2014-04-09 10:31:21 -070096 upperIndex = (i * VALUE_STORE_LIMIT + VALUE_STORE_LIMIT) - 1;
97 log.debug("writing using indexes {}:{}", (i * VALUE_STORE_LIMIT), upperIndex);
98 table.create(keyBytes.array(), Arrays.copyOfRange(buffer, i * VALUE_STORE_LIMIT, upperIndex));
Nick Karanatsiosa8800572014-02-25 01:12:06 -080099 }
100 if (remainder > 0) {
101 keyBytes.clear();
102 keyBytes.putLong(key);
103 keyBytes.flip();
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 log.debug("writing using indexes {}:{}", upperIndex, total);
Nick Karanatsiosa8800572014-02-25 01:12:06 -0800105 table.create(keyBytes.array(), Arrays.copyOfRange(buffer, upperIndex + 1, total - 1));
106 }
107 } else {
108 keyBytes.flip();
109 table.create(keyBytes.array(), buffer);
110 }
Nick Karanatsiosf9336002014-02-24 11:26:58 -0800111 log.debug("key is {} value length is {}", key, buffer.length);
112 stream.reset();
113 stream.close();
Nick Karanatsiosa1bad352014-02-22 14:16:34 -0800114 log.debug("persist operations to ramcloud size of operations: {}", operations.size());
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800115 ret = true;
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -0700116 } catch (ObjectExistsException ex) {
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800117 log.warn("Failed to store intent journal with key " + key);
Nick Karanatsiosf9336002014-02-24 11:26:58 -0800118 } catch (IOException ex) {
119 log.error("Failed to close the stream");
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800120 }
121 }
122 return ret;
123 }
124}