2 * ============LICENSE_START========================================== org.onap.music
3 * =================================================================== Copyright (c) 2017 AT&T
4 * Intellectual Property ===================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6 * in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
15 * ============LICENSE_END=============================================
16 * ====================================================================
18 package org.onap.music.rest;
20 import java.util.List;
22 import java.util.UUID;
23 import javax.ws.rs.Consumes;
24 import javax.ws.rs.GET;
25 import javax.ws.rs.POST;
26 import javax.ws.rs.PUT;
27 import javax.ws.rs.Path;
28 import javax.ws.rs.PathParam;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.core.Context;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.MultivaluedMap;
33 import javax.ws.rs.core.UriInfo;
34 import org.onap.music.datastore.jsonobjects.JsonInsert;
35 import org.onap.music.eelf.logging.EELFLoggerDelegate;
36 import org.onap.music.main.MusicCore;
37 import org.onap.music.main.MusicUtil;
38 import org.onap.music.main.ResultType;
39 import org.onap.music.main.ReturnType;
40 import org.onap.music.datastore.PreparedQueryObject;
41 import com.datastax.driver.core.DataType;
42 import com.datastax.driver.core.TableMetadata;
43 import io.swagger.annotations.Api;
44 //import io.swagger.annotations.ApiOperation;
45 //import io.swagger.annotations.ApiParam;
48 * These are functions created purely for benchmarking purposes. Commented out Swagger - This should
52 @Path("/v{version: [0-9]+}/benchmarks/")
53 @Api(value = "Benchmark API", hidden = true)
54 public class RestMusicBmAPI {
56 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicBmAPI.class);
66 @Path("/purezk/{name}")
67 @Consumes(MediaType.APPLICATION_JSON)
68 public void pureZkCreate(@PathParam("name") String nodeName) throws Exception {
69 MusicCore.pureZkCreate("/" + nodeName);
80 @Path("/purezk/{name}")
81 @Consumes(MediaType.APPLICATION_JSON)
82 public void pureZkUpdate(JsonInsert insObj, @PathParam("name") String nodeName)
84 logger.info(EELFLoggerDelegate.applicationLogger,"--------------Zk normal update-------------------------");
85 long start = System.currentTimeMillis();
86 MusicCore.pureZkWrite(nodeName, insObj.serialize());
87 long end = System.currentTimeMillis();
88 logger.info(EELFLoggerDelegate.applicationLogger,"Total time taken for Zk normal update:" + (end - start) + " ms");
98 @Path("/purezk/{name}")
99 @Consumes(MediaType.TEXT_PLAIN)
100 public byte[] pureZkGet(@PathParam("name") String nodeName) throws Exception {
101 return MusicCore.pureZkRead(nodeName);
112 @Path("/purezk/atomic/{lockname}/{name}")
113 @Consumes(MediaType.APPLICATION_JSON)
114 public void pureZkAtomicPut(JsonInsert updateObj, @PathParam("lockname") String lockname,
115 @PathParam("name") String nodeName) throws Exception {
116 long startTime = System.currentTimeMillis();
117 String operationId = UUID.randomUUID().toString();// just for debugging purposes.
118 String consistency = updateObj.getConsistencyInfo().get("type");
120 logger.info(EELFLoggerDelegate.applicationLogger,"--------------Zookeeper " + consistency + " update-" + operationId
121 + "-------------------------");
123 byte[] data = updateObj.serialize();
124 long jsonParseCompletionTime = System.currentTimeMillis();
126 String lockId = MusicCore.createLockReference(lockname);
128 long lockCreationTime = System.currentTimeMillis();
130 long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
131 ReturnType lockAcqResult = MusicCore.acquireLockWithLease(lockname, lockId, leasePeriod);
132 long lockAcqTime = System.currentTimeMillis();
133 long zkPutTime = 0, lockReleaseTime = 0;
135 if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
136 logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId);
137 MusicCore.pureZkWrite(lockname, data);
138 zkPutTime = System.currentTimeMillis();
139 boolean voluntaryRelease = true;
140 if (consistency.equals("atomic"))
141 MusicCore.releaseLock(lockId, voluntaryRelease);
142 else if (consistency.equals("atomic_delete_lock"))
143 MusicCore.deleteLock(lockname);
144 lockReleaseTime = System.currentTimeMillis();
146 MusicCore.destroyLockRef(lockId);
149 long actualUpdateCompletionTime = System.currentTimeMillis();
152 long endTime = System.currentTimeMillis();
154 String lockingInfo = "|lock creation time:" + (lockCreationTime - jsonParseCompletionTime)
155 + "|lock accquire time:" + (lockAcqTime - lockCreationTime)
156 + "|zk put time:" + (zkPutTime - lockAcqTime);
158 if (consistency.equals("atomic"))
159 lockingInfo = lockingInfo + "|lock release time:" + (lockReleaseTime - zkPutTime) + "|";
160 else if (consistency.equals("atomic_delete_lock"))
161 lockingInfo = lockingInfo + "|lock delete time:" + (lockReleaseTime - zkPutTime) + "|";
163 String timingString = "Time taken in ms for Zookeeper " + consistency + " update-"
164 + operationId + ":" + "|total operation time:" + (endTime - startTime)
165 + "|json parsing time:" + (jsonParseCompletionTime - startTime)
166 + "|update time:" + (actualUpdateCompletionTime - jsonParseCompletionTime)
169 logger.info(EELFLoggerDelegate.applicationLogger,timingString);
180 @Path("/purezk/atomic/{lockname}/{name}")
181 @Consumes(MediaType.APPLICATION_JSON)
182 public void pureZkAtomicGet(JsonInsert insObj, @PathParam("lockname") String lockName,
183 @PathParam("name") String nodeName) throws Exception {
184 logger.info("--------------Zk atomic read-------------------------");
185 long start = System.currentTimeMillis();
186 String lockId = MusicCore.createLockReference(lockName);
187 long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
188 ReturnType lockAcqResult = MusicCore.acquireLockWithLease(lockName, lockId, leasePeriod);
189 if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
190 logger.info("acquired lock with id " + lockId);
191 MusicCore.pureZkRead(nodeName);
192 boolean voluntaryRelease = true;
193 MusicCore.releaseLock(lockId, voluntaryRelease);
195 MusicCore.destroyLockRef(lockId);
198 long end = System.currentTimeMillis();
199 logger.info(EELFLoggerDelegate.applicationLogger,"Total time taken for Zk atomic read:" + (end - start) + " ms");
204 * doing an update directly to cassa but through the rest api
214 @Path("/cassa/keyspaces/{keyspace}/tables/{tablename}/rows")
215 @Consumes(MediaType.APPLICATION_JSON)
216 @Produces(MediaType.APPLICATION_JSON)
217 public boolean updateTableCassa(JsonInsert insObj, @PathParam("keyspace") String keyspace,
218 @PathParam("tablename") String tablename, @Context UriInfo info)
220 long startTime = System.currentTimeMillis();
221 String operationId = UUID.randomUUID().toString();// just for debugging purposes.
222 String consistency = insObj.getConsistencyInfo().get("type");
223 logger.info(EELFLoggerDelegate.applicationLogger,"--------------Cassandra " + consistency + " update-" + operationId
224 + "-------------------------");
225 PreparedQueryObject queryObject = new PreparedQueryObject();
226 Map<String, Object> valuesMap = insObj.getValues();
227 TableMetadata tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename);
228 String vectorTs = "'" + Thread.currentThread().getId() + System.currentTimeMillis() + "'";
229 String fieldValueString = "vector_ts= ? ,";
230 queryObject.addValue(vectorTs);
233 for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
234 Object valueObj = entry.getValue();
235 DataType colType = tableInfo.getColumn(entry.getKey()).getType();
236 Object valueString = MusicUtil.convertToActualDataType(colType, valueObj);
237 fieldValueString = fieldValueString + entry.getKey() + "= ?";
238 queryObject.addValue(valueString);
239 if (counter != valuesMap.size() - 1)
240 fieldValueString = fieldValueString + ",";
241 counter = counter + 1;
244 // get the row specifier
247 queryObject.appendQueryString("UPDATE " + keyspace + "." + tablename + " ");
248 MultivaluedMap<String, String> rowParams = info.getQueryParameters();
249 String primaryKey = "";
250 for (MultivaluedMap.Entry<String, List<String>> entry : rowParams.entrySet()) {
251 String keyName = entry.getKey();
252 List<String> valueList = entry.getValue();
253 String indValue = valueList.get(0);
254 DataType colType = tableInfo.getColumn(entry.getKey()).getType();
255 Object formattedValue = MusicUtil.convertToActualDataType(colType, indValue);
256 primaryKey = primaryKey + indValue;
257 rowSpec = rowSpec + keyName + "= ? ";
258 queryObject.addValue(formattedValue);
259 if (counter != rowParams.size() - 1)
260 rowSpec = rowSpec + " AND ";
261 counter = counter + 1;
265 String ttl = insObj.getTtl();
266 String timestamp = insObj.getTimestamp();
268 if ((ttl != null) && (timestamp != null)) {
270 logger.info(EELFLoggerDelegate.applicationLogger,"both there");
271 queryObject.appendQueryString(" USING TTL ? AND TIMESTAMP ?");
272 queryObject.addValue(Integer.parseInt(ttl));
273 queryObject.addValue(Long.parseLong(timestamp));
276 if ((ttl != null) && (timestamp == null)) {
277 logger.info(EELFLoggerDelegate.applicationLogger,"ONLY TTL there");
278 queryObject.appendQueryString(" USING TTL ?");
279 queryObject.addValue(Integer.parseInt(ttl));
282 if ((ttl == null) && (timestamp != null)) {
283 logger.info(EELFLoggerDelegate.applicationLogger,"ONLY timestamp there");
284 queryObject.appendQueryString(" USING TIMESTAMP ?");
285 queryObject.addValue(Long.parseLong(timestamp));
287 queryObject.appendQueryString(" SET " + fieldValueString + " WHERE " + rowSpec + ";");
289 long jsonParseCompletionTime = System.currentTimeMillis();
291 boolean operationResult = true;
292 MusicCore.getDSHandle().executePut(queryObject, insObj.getConsistencyInfo().get("type"));
294 long actualUpdateCompletionTime = System.currentTimeMillis();
296 long endTime = System.currentTimeMillis();
298 String timingString = "Time taken in ms for Cassandra " + consistency + " update-"
299 + operationId + ":" + "|total operation time:" + (endTime - startTime)
300 + "|json parsing time:" + (jsonParseCompletionTime - startTime)
301 + "|update time:" + (actualUpdateCompletionTime - jsonParseCompletionTime)
303 logger.info(EELFLoggerDelegate.applicationLogger,timingString);
305 return operationResult;