Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent.runtime; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 2 | |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 3 | import java.io.ByteArrayOutputStream; |
Nick Karanatsios | f933600 | 2014-02-24 11:26:58 -0800 | [diff] [blame] | 4 | import java.io.IOException; |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame] | 5 | import java.nio.ByteBuffer; |
| 6 | import java.util.Arrays; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 7 | import java.util.concurrent.atomic.AtomicLong; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 8 | |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.datagrid.web.IntentResource; |
| 10 | import net.onrc.onos.core.datastore.DataStoreClient; |
| 11 | import net.onrc.onos.core.datastore.IKVTable; |
| 12 | import net.onrc.onos.core.datastore.ObjectExistsException; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 13 | import net.onrc.onos.core.intent.IntentOperationList; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 14 | import net.onrc.onos.core.registry.IControllerRegistryService; |
| 15 | import net.onrc.onos.core.registry.IdBlock; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 16 | import net.onrc.onos.core.util.serializers.KryoFactory; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 17 | |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 18 | import org.slf4j.Logger; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 19 | import org.slf4j.LoggerFactory; |
| 20 | |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 21 | import com.esotericsoftware.kryo.Kryo; |
| 22 | import com.esotericsoftware.kryo.io.Output; |
| 23 | |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 24 | /** |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 25 | * @author nickkaranatsios |
| 26 | */ |
| 27 | public class PersistIntent { |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 28 | private static final Logger log = LoggerFactory.getLogger(IntentResource.class); |
Nick Karanatsios | f933600 | 2014-02-24 11:26:58 -0800 | [diff] [blame] | 29 | private long range = 10000L; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 30 | private final IControllerRegistryService controllerRegistry; |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 31 | private static final String INTENT_JOURNAL = "G:IntentJournal"; |
| 32 | private static final int VALUE_STORE_LIMIT = 1024 * 1024; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 33 | private IKVTable table; |
Nick Karanatsios | d1dfb92 | 2014-02-26 11:48:49 -0800 | [diff] [blame] | 34 | private Kryo kryo; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 35 | private ByteArrayOutputStream stream; |
| 36 | private Output output = null; |
| 37 | private AtomicLong nextId = null; |
| 38 | private long rangeEnd; |
| 39 | private IdBlock idBlock = null; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 40 | |
| 41 | |
Pavlin Radoslavov | 0294e05 | 2014-04-10 13:36:45 -0700 | [diff] [blame] | 42 | public PersistIntent(final IControllerRegistryService controllerRegistry) { |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 43 | this.controllerRegistry = controllerRegistry; |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 44 | table = DataStoreClient.getClient().getTable(INTENT_JOURNAL); |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 45 | stream = new ByteArrayOutputStream(1024); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 46 | output = new Output(stream); |
Nick Karanatsios | d1dfb92 | 2014-02-26 11:48:49 -0800 | [diff] [blame] | 47 | kryo = (new KryoFactory()).newKryo(); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 48 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 49 | |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 50 | 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 HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 62 | |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 63 | private long getNextBlock() { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 64 | // XXX This method is not thread safe, may lose allocated IdBlock |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 65 | idBlock = controllerRegistry.allocateUniqueIdBlock(range); |
| 66 | nextId = new AtomicLong(idBlock.getStart()); |
| 67 | rangeEnd = idBlock.getEnd(); |
| 68 | return nextId.get(); |
| 69 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 70 | |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 71 | 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 Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame] | 77 | // reserve key 10 entries for multi-write if size over 1MB |
| 78 | key *= 10; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 79 | kryo.writeObject(output, operations); |
| 80 | output.close(); |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame] | 81 | ByteBuffer keyBytes = ByteBuffer.allocate(8).putLong(key); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 82 | byte[] buffer = stream.toByteArray(); |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame] | 83 | int total = buffer.length; |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 84 | if ((total >= VALUE_STORE_LIMIT)) { |
| 85 | int writeCount = total / VALUE_STORE_LIMIT; |
| 86 | int remainder = total % VALUE_STORE_LIMIT; |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame] | 87 | int upperIndex = 0; |
| 88 | for (int i = 0; i < writeCount; i++, key++) { |
| 89 | keyBytes.clear(); |
| 90 | keyBytes.putLong(key); |
| 91 | keyBytes.flip(); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 92 | 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 Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame] | 95 | } |
| 96 | if (remainder > 0) { |
| 97 | keyBytes.clear(); |
| 98 | keyBytes.putLong(key); |
| 99 | keyBytes.flip(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 100 | log.debug("writing using indexes {}:{}", upperIndex, total); |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame] | 101 | 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 Karanatsios | f933600 | 2014-02-24 11:26:58 -0800 | [diff] [blame] | 107 | log.debug("key is {} value length is {}", key, buffer.length); |
| 108 | stream.reset(); |
| 109 | stream.close(); |
Nick Karanatsios | a1bad35 | 2014-02-22 14:16:34 -0800 | [diff] [blame] | 110 | log.debug("persist operations to ramcloud size of operations: {}", operations.size()); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 111 | ret = true; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 112 | } catch (ObjectExistsException ex) { |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 113 | log.warn("Failed to store intent journal with key " + key); |
Nick Karanatsios | f933600 | 2014-02-24 11:26:58 -0800 | [diff] [blame] | 114 | } catch (IOException ex) { |
| 115 | log.error("Failed to close the stream"); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | return ret; |
| 119 | } |
| 120 | } |