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