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