Madan Jampani
Committed by Gerrit Code Review

ONOS-2088: Lazy id block allocation

Change-Id: Ie99d2ba017f45f1ca99b04da05cdb06b41202d3a
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
15 */ 15 */
16 package org.onosproject.core.impl; 16 package org.onosproject.core.impl;
17 17
18 +import java.util.concurrent.atomic.AtomicBoolean;
19 +
18 import org.onosproject.core.IdBlock; 20 import org.onosproject.core.IdBlock;
19 import org.onosproject.core.IdGenerator; 21 import org.onosproject.core.IdGenerator;
20 import org.onosproject.core.UnavailableIdException; 22 import org.onosproject.core.UnavailableIdException;
...@@ -26,6 +28,8 @@ import org.onosproject.core.UnavailableIdException; ...@@ -26,6 +28,8 @@ import org.onosproject.core.UnavailableIdException;
26 public class BlockAllocatorBasedIdGenerator implements IdGenerator { 28 public class BlockAllocatorBasedIdGenerator implements IdGenerator {
27 protected final IdBlockAllocator allocator; 29 protected final IdBlockAllocator allocator;
28 protected IdBlock idBlock; 30 protected IdBlock idBlock;
31 + protected AtomicBoolean initialized;
32 +
29 33
30 /** 34 /**
31 * Constructs an ID generator which use {@link IdBlockAllocator} as backend. 35 * Constructs an ID generator which use {@link IdBlockAllocator} as backend.
...@@ -34,18 +38,26 @@ public class BlockAllocatorBasedIdGenerator implements IdGenerator { ...@@ -34,18 +38,26 @@ public class BlockAllocatorBasedIdGenerator implements IdGenerator {
34 */ 38 */
35 protected BlockAllocatorBasedIdGenerator(IdBlockAllocator allocator) { 39 protected BlockAllocatorBasedIdGenerator(IdBlockAllocator allocator) {
36 this.allocator = allocator; 40 this.allocator = allocator;
37 - this.idBlock = allocator.allocateUniqueIdBlock(); 41 + this.initialized = new AtomicBoolean(false);
38 } 42 }
39 43
40 @Override 44 @Override
41 public long getNewId() { 45 public long getNewId() {
42 try { 46 try {
47 + if (!initialized.get()) {
48 + synchronized (allocator) {
49 + if (!initialized.get()) {
50 + idBlock = allocator.allocateUniqueIdBlock();
51 + initialized.set(true);
52 + }
53 + }
54 + }
43 return idBlock.getNextId(); 55 return idBlock.getNextId();
44 } catch (UnavailableIdException e) { 56 } catch (UnavailableIdException e) {
45 synchronized (allocator) { 57 synchronized (allocator) {
46 idBlock = allocator.allocateUniqueIdBlock(); 58 idBlock = allocator.allocateUniqueIdBlock();
47 - return idBlock.getNextId();
48 } 59 }
60 + return idBlock.getNextId();
49 } 61 }
50 } 62 }
51 } 63 }
......