Murat Parlakisik
Committed by Gerrit Code Review

Shared system services configurability (ONOS-1304)

Change-Id: I42210128fd973f16bb07955175d8e361858a9034
...@@ -20,21 +20,28 @@ import org.apache.felix.scr.annotations.Component; ...@@ -20,21 +20,28 @@ import org.apache.felix.scr.annotations.Component;
20 import org.apache.felix.scr.annotations.Reference; 20 import org.apache.felix.scr.annotations.Reference;
21 import org.apache.felix.scr.annotations.ReferenceCardinality; 21 import org.apache.felix.scr.annotations.ReferenceCardinality;
22 import org.apache.felix.scr.annotations.Service; 22 import org.apache.felix.scr.annotations.Service;
23 +import org.apache.felix.scr.annotations.Property;
24 +import org.apache.felix.scr.annotations.Deactivate;
25 +import org.apache.felix.scr.annotations.Modified;
26 +import org.onlab.util.SharedExecutors;
23 import org.onlab.util.Tools; 27 import org.onlab.util.Tools;
28 +import org.onosproject.cfg.ComponentConfigService;
24 import org.onosproject.core.ApplicationId; 29 import org.onosproject.core.ApplicationId;
25 import org.onosproject.core.ApplicationIdStore; 30 import org.onosproject.core.ApplicationIdStore;
26 import org.onosproject.core.CoreService; 31 import org.onosproject.core.CoreService;
27 import org.onosproject.core.IdBlockStore; 32 import org.onosproject.core.IdBlockStore;
28 import org.onosproject.core.IdGenerator; 33 import org.onosproject.core.IdGenerator;
29 import org.onosproject.core.Version; 34 import org.onosproject.core.Version;
35 +import org.osgi.service.component.ComponentContext;
30 import org.slf4j.Logger; 36 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory; 37 import org.slf4j.LoggerFactory;
32 -
33 import java.io.File; 38 import java.io.File;
39 +import java.util.Dictionary;
34 import java.util.List; 40 import java.util.List;
35 import java.util.Set; 41 import java.util.Set;
36 42
37 import static com.google.common.base.Preconditions.checkNotNull; 43 import static com.google.common.base.Preconditions.checkNotNull;
44 +import static com.google.common.base.Strings.isNullOrEmpty;
38 45
39 /** 46 /**
40 * Core service implementation. 47 * Core service implementation.
...@@ -54,14 +61,27 @@ public class CoreManager implements CoreService { ...@@ -54,14 +61,27 @@ public class CoreManager implements CoreService {
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected IdBlockStore idBlockStore; 62 protected IdBlockStore idBlockStore;
56 63
64 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 + protected ComponentConfigService cfgService;
66 +
67 + @Property(name = "sharedThreadPoolSize", intValue = SharedExecutors.DEFAULT_THREAD_SIZE,
68 + label = "Configure shared pool maximum size ")
69 + private int sharedThreadPoolSize = SharedExecutors.DEFAULT_THREAD_SIZE;
70 +
57 @Activate 71 @Activate
58 public void activate() { 72 public void activate() {
73 + cfgService.registerProperties(getClass());
59 List<String> versionLines = Tools.slurp(VERSION_FILE); 74 List<String> versionLines = Tools.slurp(VERSION_FILE);
60 if (versionLines != null && !versionLines.isEmpty()) { 75 if (versionLines != null && !versionLines.isEmpty()) {
61 version = Version.version(versionLines.get(0)); 76 version = Version.version(versionLines.get(0));
62 } 77 }
63 } 78 }
64 79
80 + @Deactivate
81 + public void deactivate() {
82 + cfgService.unregisterProperties(getClass(), false);
83 + }
84 +
65 @Override 85 @Override
66 public Version version() { 86 public Version version() {
67 return version; 87 return version;
...@@ -95,4 +115,48 @@ public class CoreManager implements CoreService { ...@@ -95,4 +115,48 @@ public class CoreManager implements CoreService {
95 return new BlockAllocatorBasedIdGenerator(allocator); 115 return new BlockAllocatorBasedIdGenerator(allocator);
96 } 116 }
97 117
118 +
119 + @Modified
120 + public void modified(ComponentContext context) {
121 + Dictionary<?, ?> properties = context.getProperties();
122 + Integer sharedThreadPoolSizeConfig =
123 + getIntegerProperty(properties, "sharedThreadPoolSize");
124 + if (sharedThreadPoolSizeConfig == null) {
125 + log.info("Shared Pool Size is not configured, default value is {}",
126 + sharedThreadPoolSize);
127 + } else {
128 + if (sharedThreadPoolSizeConfig > 0) {
129 + sharedThreadPoolSize = sharedThreadPoolSizeConfig;
130 + SharedExecutors.setPoolSize(sharedThreadPoolSize);
131 + log.info("Configured. Shared Pool Size is configured to {}",
132 + sharedThreadPoolSize);
133 + } else {
134 + log.warn("Shared Pool Size size must be greater than 0");
135 + }
136 + }
137 + }
138 +
139 +
140 +
141 + /**
142 + * Get Integer property from the propertyName
143 + * Return null if propertyName is not found.
144 + *
145 + * @param properties properties to be looked up
146 + * @param propertyName the name of the property to look up
147 + * @return value when the propertyName is defined or return null
148 + */
149 + private static Integer getIntegerProperty(Dictionary<?, ?> properties,
150 + String propertyName) {
151 + Integer value = null;
152 + try {
153 + String s = (String) properties.get(propertyName);
154 + value = isNullOrEmpty(s) ? value : Integer.parseInt(s.trim());
155 + } catch (NumberFormatException | ClassCastException e) {
156 + value = null;
157 + }
158 + return value;
159 + }
160 +
161 +
98 } 162 }
......
...@@ -18,6 +18,8 @@ package org.onlab.util; ...@@ -18,6 +18,8 @@ package org.onlab.util;
18 18
19 import java.util.Timer; 19 import java.util.Timer;
20 import java.util.concurrent.ExecutorService; 20 import java.util.concurrent.ExecutorService;
21 +import org.slf4j.Logger;
22 +import org.slf4j.LoggerFactory;
21 23
22 import static java.util.concurrent.Executors.newFixedThreadPool; 24 import static java.util.concurrent.Executors.newFixedThreadPool;
23 import static java.util.concurrent.Executors.newSingleThreadExecutor; 25 import static java.util.concurrent.Executors.newSingleThreadExecutor;
...@@ -34,15 +36,18 @@ import static org.onlab.util.Tools.groupedThreads; ...@@ -34,15 +36,18 @@ import static org.onlab.util.Tools.groupedThreads;
34 */ 36 */
35 public final class SharedExecutors { 37 public final class SharedExecutors {
36 38
39 + private static final Logger log = LoggerFactory.getLogger(SharedExecutors.class);
40 +
37 // TODO: make this configurable via setPoolSize static method 41 // TODO: make this configurable via setPoolSize static method
38 - private static int numberOfThreads = 30; 42 + public static final int DEFAULT_THREAD_SIZE = 30;
43 + private static int poolSize = DEFAULT_THREAD_SIZE;
39 44
40 private static ExecutorService singleThreadExecutor = 45 private static ExecutorService singleThreadExecutor =
41 newSingleThreadExecutor(groupedThreads("onos/shared", 46 newSingleThreadExecutor(groupedThreads("onos/shared",
42 "onos-single-executor")); 47 "onos-single-executor"));
43 48
44 private static ExecutorService poolThreadExecutor = 49 private static ExecutorService poolThreadExecutor =
45 - newFixedThreadPool(numberOfThreads, groupedThreads("onos/shared", 50 + newFixedThreadPool(poolSize, groupedThreads("onos/shared",
46 "onos-pool-executor-%d")); 51 "onos-pool-executor-%d"));
47 52
48 private static Timer sharedTimer = new Timer("onos-shared-timer"); 53 private static Timer sharedTimer = new Timer("onos-shared-timer");
...@@ -78,4 +83,19 @@ public final class SharedExecutors { ...@@ -78,4 +83,19 @@ public final class SharedExecutors {
78 return sharedTimer; 83 return sharedTimer;
79 } 84 }
80 85
86 + /**
87 + * Sets the shared thread pool size.
88 + * @param poolSize
89 + */
90 + public static void setPoolSize(int poolSize) {
91 + if (poolSize > 0) {
92 + SharedExecutors.poolSize = poolSize;
93 + //TODO: wait for execution previous task in the queue .
94 + poolThreadExecutor.shutdown();
95 + poolThreadExecutor = newFixedThreadPool(poolSize, groupedThreads("onos/shared",
96 + "onos-pool-executor-%d"));
97 + } else {
98 + log.warn("Shared Pool Size size must be greater than 0");
99 + }
100 + }
81 } 101 }
......