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.datastore.DataStoreClient; |
| 10 | import net.onrc.onos.core.datastore.IKVTable; |
| 11 | import net.onrc.onos.core.datastore.ObjectExistsException; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.intent.IntentOperationList; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 13 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Sho SHIMIZU | fc932d5 | 2014-08-15 11:22:37 -0700 | [diff] [blame] | 14 | import net.onrc.onos.core.util.IdBlock; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 15 | import net.onrc.onos.core.util.serializers.KryoFactory; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 16 | |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 17 | import org.slf4j.Logger; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 18 | import org.slf4j.LoggerFactory; |
| 19 | |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 20 | import com.esotericsoftware.kryo.Kryo; |
| 21 | import com.esotericsoftware.kryo.io.Output; |
| 22 | |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 23 | /** |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 24 | * The module used by PathCalcRuntimeModule class. |
| 25 | * <p> |
| 26 | * It persists intent operations into persistent storage. |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 27 | */ |
| 28 | public class PersistIntent { |
Pavlin Radoslavov | 0c14b8a | 2014-05-06 16:23:48 -0700 | [diff] [blame] | 29 | private static final Logger log = LoggerFactory.getLogger(PersistIntent.class); |
Nick Karanatsios | f933600 | 2014-02-24 11:26:58 -0800 | [diff] [blame] | 30 | private long range = 10000L; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 31 | private final IControllerRegistryService controllerRegistry; |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 32 | private static final String INTENT_JOURNAL = "G:IntentJournal"; |
| 33 | private static final int VALUE_STORE_LIMIT = 1024 * 1024; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 34 | private IKVTable table; |
Nick Karanatsios | d1dfb92 | 2014-02-26 11:48:49 -0800 | [diff] [blame] | 35 | private Kryo kryo; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 36 | private ByteArrayOutputStream stream; |
| 37 | private Output output = null; |
| 38 | private AtomicLong nextId = null; |
| 39 | private long rangeEnd; |
| 40 | private IdBlock idBlock = null; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 41 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Constructor. |
| 44 | * |
| 45 | * @param controllerRegistry the Registry Service to use. |
| 46 | */ |
Pavlin Radoslavov | 0294e05 | 2014-04-10 13:36:45 -0700 | [diff] [blame] | 47 | public PersistIntent(final IControllerRegistryService controllerRegistry) { |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 48 | this.controllerRegistry = controllerRegistry; |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 49 | table = DataStoreClient.getClient().getTable(INTENT_JOURNAL); |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 50 | stream = new ByteArrayOutputStream(1024); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 51 | output = new Output(stream); |
Yuta HIGUCHI | e57e10e | 2014-08-20 14:25:30 -0700 | [diff] [blame] | 52 | kryo = KryoFactory.newKryoObject(); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 53 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 54 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 55 | private long getNextBlock() { |
| 56 | // XXX This method is not thread safe, may lose allocated IdBlock |
| 57 | idBlock = controllerRegistry.allocateUniqueIdBlock(range); |
| 58 | nextId = new AtomicLong(idBlock.getStart()); |
| 59 | rangeEnd = idBlock.getEnd(); |
| 60 | return nextId.get(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Provides the unique key for persisting. |
| 65 | * <p> |
| 66 | * This key is necessary for persistIfLeader() method. |
| 67 | * |
| 68 | * @return a key for persisting. |
| 69 | */ |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 70 | public long getKey() { |
| 71 | long key; |
| 72 | if (idBlock == null) { |
| 73 | key = getNextBlock(); |
| 74 | } else { |
| 75 | key = nextId.incrementAndGet(); |
| 76 | if (key >= rangeEnd) { |
| 77 | key = getNextBlock(); |
| 78 | } |
| 79 | } |
| 80 | return key; |
| 81 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 82 | |
Toshio Koide | fdb7593 | 2014-06-16 17:59:24 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Persist intent operations into persistent storage only if this instance was a leader. |
| 85 | * |
| 86 | * @param key a unique key |
| 87 | * @param operations intent operations |
| 88 | * @return true if succeeded, otherwise false. |
| 89 | */ |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 90 | public boolean persistIfLeader(long key, IntentOperationList operations) { |
| 91 | boolean leader = true; |
| 92 | boolean ret = false; |
Ray Milkey | 4373cbe | 2014-08-12 09:58:58 -0700 | [diff] [blame] | 93 | long keyValue = key; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 94 | // TODO call controllerRegistry.isClusterLeader() |
| 95 | if (leader) { |
| 96 | try { |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame] | 97 | // reserve key 10 entries for multi-write if size over 1MB |
Ray Milkey | 4373cbe | 2014-08-12 09:58:58 -0700 | [diff] [blame] | 98 | keyValue *= 10; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 99 | kryo.writeObject(output, operations); |
| 100 | output.close(); |
Ray Milkey | 4373cbe | 2014-08-12 09:58:58 -0700 | [diff] [blame] | 101 | ByteBuffer keyBytes = ByteBuffer.allocate(8).putLong(keyValue); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 102 | byte[] buffer = stream.toByteArray(); |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame] | 103 | int total = buffer.length; |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 104 | if ((total >= VALUE_STORE_LIMIT)) { |
| 105 | int writeCount = total / VALUE_STORE_LIMIT; |
| 106 | int remainder = total % VALUE_STORE_LIMIT; |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame] | 107 | int upperIndex = 0; |
Ray Milkey | 4373cbe | 2014-08-12 09:58:58 -0700 | [diff] [blame] | 108 | for (int i = 0; i < writeCount; i++, keyValue++) { |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame] | 109 | keyBytes.clear(); |
Ray Milkey | 4373cbe | 2014-08-12 09:58:58 -0700 | [diff] [blame] | 110 | keyBytes.putLong(keyValue); |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame] | 111 | keyBytes.flip(); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 112 | upperIndex = (i * VALUE_STORE_LIMIT + VALUE_STORE_LIMIT) - 1; |
| 113 | log.debug("writing using indexes {}:{}", (i * VALUE_STORE_LIMIT), upperIndex); |
| 114 | table.create(keyBytes.array(), Arrays.copyOfRange(buffer, i * VALUE_STORE_LIMIT, upperIndex)); |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame] | 115 | } |
| 116 | if (remainder > 0) { |
| 117 | keyBytes.clear(); |
Ray Milkey | 4373cbe | 2014-08-12 09:58:58 -0700 | [diff] [blame] | 118 | keyBytes.putLong(keyValue); |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame] | 119 | keyBytes.flip(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 120 | log.debug("writing using indexes {}:{}", upperIndex, total); |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame] | 121 | table.create(keyBytes.array(), Arrays.copyOfRange(buffer, upperIndex + 1, total - 1)); |
| 122 | } |
| 123 | } else { |
| 124 | keyBytes.flip(); |
| 125 | table.create(keyBytes.array(), buffer); |
| 126 | } |
Ray Milkey | 4373cbe | 2014-08-12 09:58:58 -0700 | [diff] [blame] | 127 | log.debug("key is {} value length is {}", keyValue, buffer.length); |
Nick Karanatsios | f933600 | 2014-02-24 11:26:58 -0800 | [diff] [blame] | 128 | stream.reset(); |
| 129 | stream.close(); |
Nick Karanatsios | a1bad35 | 2014-02-22 14:16:34 -0800 | [diff] [blame] | 130 | log.debug("persist operations to ramcloud size of operations: {}", operations.size()); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 131 | ret = true; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 132 | } catch (ObjectExistsException ex) { |
Ray Milkey | 4373cbe | 2014-08-12 09:58:58 -0700 | [diff] [blame] | 133 | log.warn("Failed to store intent journal with key " + keyValue); |
Nick Karanatsios | f933600 | 2014-02-24 11:26:58 -0800 | [diff] [blame] | 134 | } catch (IOException ex) { |
| 135 | log.error("Failed to close the stream"); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | return ret; |
| 139 | } |
| 140 | } |