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 * ====================================================================
19 package org.onap.music.rest;
21 import java.util.List;
23 import java.util.UUID;
24 import javax.ws.rs.Consumes;
25 import javax.ws.rs.GET;
26 import javax.ws.rs.POST;
27 import javax.ws.rs.PUT;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.PathParam;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.core.Context;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.MultivaluedMap;
34 import javax.ws.rs.core.UriInfo;
35 import org.onap.music.datastore.jsonobjects.JsonInsert;
36 import org.onap.music.eelf.logging.EELFLoggerDelegate;
37 import org.onap.music.main.MusicCore;
38 import org.onap.music.main.MusicUtil;
39 import org.onap.music.main.ResultType;
40 import org.onap.music.main.ReturnType;
41 import org.onap.music.datastore.PreparedQueryObject;
42 import com.datastax.driver.core.DataType;
43 import com.datastax.driver.core.TableMetadata;
44 import io.swagger.annotations.Api;
45 //import io.swagger.annotations.ApiOperation;
46 //import io.swagger.annotations.ApiParam;
49 * These are functions created purely for benchmarking purposes. Commented out Swagger - This should
53 @Path("/v{version: [0-9]+}/benchmarks/")
54 @Api(value = "Benchmark API", hidden = true)
55 public class RestMusicBmAPI {
57 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicBmAPI.class);
67 @Path("/purezk/{name}")
68 @Consumes(MediaType.APPLICATION_JSON)
69 public void pureZkCreate(@PathParam("name") String nodeName) throws Exception {
70 MusicCore.pureZkCreate("/" + nodeName);
81 @Path("/purezk/{name}")
82 @Consumes(MediaType.APPLICATION_JSON)
83 public void pureZkUpdate(JsonInsert insObj, @PathParam("name") String nodeName)
85 logger.info(EELFLoggerDelegate.applicationLogger,"--------------Zk normal update-------------------------");
86 long start = System.currentTimeMillis();
87 MusicCore.pureZkWrite(nodeName, insObj.serialize());
88 long end = System.currentTimeMillis();
89 logger.info(EELFLoggerDelegate.applicationLogger,"Total time taken for Zk normal update:" + (end - start) + " ms");
99 @Path("/purezk/{name}")
100 @Consumes(MediaType.TEXT_PLAIN)
101 public byte[] pureZkGet(@PathParam("name") String nodeName) throws Exception {
102 return MusicCore.pureZkRead(nodeName);
113 @Path("/purezk/atomic/{lockname}/{name}")
114 @Consumes(MediaType.APPLICATION_JSON)
115 public void pureZkAtomicPut(JsonInsert updateObj, @PathParam("lockname") String lockname,
116 @PathParam("name") String nodeName) throws Exception {
117 long startTime = System.currentTimeMillis();
118 String operationId = UUID.randomUUID().toString();// just for debugging purposes.
119 String consistency = updateObj.getConsistencyInfo().get("type");
121 logger.info(EELFLoggerDelegate.applicationLogger,"--------------Zookeeper " + consistency + " update-" + operationId
122 + "-------------------------");
124 byte[] data = updateObj.serialize();
125 long jsonParseCompletionTime = System.currentTimeMillis();
127 String lockId = MusicCore.createLockReference(lockname);
129 long lockCreationTime = System.currentTimeMillis();
131 long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
132 ReturnType lockAcqResult = MusicCore.acquireLockWithLease(lockname, lockId, leasePeriod);
133 long lockAcqTime = System.currentTimeMillis();
134 long zkPutTime = 0, lockReleaseTime = 0;
136 if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
137 logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId);
138 MusicCore.pureZkWrite(lockname, data);
139 zkPutTime = System.currentTimeMillis();
140 boolean voluntaryRelease = true;
141 if (consistency.equals("atomic"))
142 MusicCore.releaseLock(lockId, voluntaryRelease);
143 else if (consistency.equals("atomic_delete_lock"))
144 MusicCore.deleteLock(lockname);
145 lockReleaseTime = System.currentTimeMillis();
147 MusicCore.destroyLockRef(lockId);
150 long actualUpdateCompletionTime = System.currentTimeMillis();
153 long endTime = System.currentTimeMillis();
155 String lockingInfo = "|lock creation time:" + (lockCreationTime - jsonParseCompletionTime)
156 + "|lock accquire time:" + (lockAcqTime - lockCreationTime)
157 + "|zk put time:" + (zkPutTime - lockAcqTime);
159 if (consistency.equals("atomic"))
160 lockingInfo = lockingInfo + "|lock release time:" + (lockReleaseTime - zkPutTime) + "|";
161 else if (consistency.equals("atomic_delete_lock"))
162 lockingInfo = lockingInfo + "|lock delete time:" + (lockReleaseTime - zkPutTime) + "|";
164 String timingString = "Time taken in ms for Zookeeper " + consistency + " update-"
165 + operationId + ":" + "|total operation time:" + (endTime - startTime)
166 + "|json parsing time:" + (jsonParseCompletionTime - startTime)
167 + "|update time:" + (actualUpdateCompletionTime - jsonParseCompletionTime)
170 logger.info(EELFLoggerDelegate.applicationLogger,timingString);
181 @Path("/purezk/atomic/{lockname}/{name}")
182 @Consumes(MediaType.APPLICATION_JSON)
183 public void pureZkAtomicGet(JsonInsert insObj, @PathParam("lockname") String lockName,
184 @PathParam("name") String nodeName) throws Exception {
185 logger.info("--------------Zk atomic read-------------------------");
186 long start = System.currentTimeMillis();
187 String lockId = MusicCore.createLockReference(lockName);
188 long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
189 ReturnType lockAcqResult = MusicCore.acquireLockWithLease(lockName, lockId, leasePeriod);
190 if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
191 logger.info("acquired lock with id " + lockId);
192 MusicCore.pureZkRead(nodeName);
193 boolean voluntaryRelease = true;
194 MusicCore.releaseLock(lockId, voluntaryRelease);
196 MusicCore.destroyLockRef(lockId);
199 long end = System.currentTimeMillis();
200 logger.info(EELFLoggerDelegate.applicationLogger,"Total time taken for Zk atomic read:" + (end - start) + " ms");
205 * doing an update directly to cassa but through the rest api
215 @Path("/cassa/keyspaces/{keyspace}/tables/{tablename}/rows")
216 @Consumes(MediaType.APPLICATION_JSON)
217 @Produces(MediaType.APPLICATION_JSON)
218 public boolean updateTableCassa(JsonInsert insObj, @PathParam("keyspace") String keyspace,
219 @PathParam("tablename") String tablename, @Context UriInfo info)
221 long startTime = System.currentTimeMillis();
222 String operationId = UUID.randomUUID().toString();// just for debugging purposes.
223 String consistency = insObj.getConsistencyInfo().get("type");
224 logger.info(EELFLoggerDelegate.applicationLogger,"--------------Cassandra " + consistency + " update-" + operationId
225 + "-------------------------");
226 PreparedQueryObject queryObject = new PreparedQueryObject();
227 Map<String, Object> valuesMap = insObj.getValues();
228 TableMetadata tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename);
229 String vectorTs = "'" + Thread.currentThread().getId() + System.currentTimeMillis() + "'";
230 String fieldValueString = "vector_ts= ? ,";
231 queryObject.addValue(vectorTs);
234 for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
235 Object valueObj = entry.getValue();
236 DataType colType = tableInfo.getColumn(entry.getKey()).getType();
237 Object valueString = MusicUtil.convertToActualDataType(colType, valueObj);
238 fieldValueString = fieldValueString + entry.getKey() + "= ?";
239 queryObject.addValue(valueString);
240 if (counter != valuesMap.size() - 1)
241 fieldValueString = fieldValueString + ",";
242 counter = counter + 1;
245 // get the row specifier
248 queryObject.appendQueryString("UPDATE " + keyspace + "." + tablename + " ");
249 MultivaluedMap<String, String> rowParams = info.getQueryParameters();
250 String primaryKey = "";
251 for (MultivaluedMap.Entry<String, List<String>> entry : rowParams.entrySet()) {
252 String keyName = entry.getKey();
253 List<String> valueList = entry.getValue();
254 String indValue = valueList.get(0);
255 DataType colType = tableInfo.getColumn(entry.getKey()).getType();
256 Object formattedValue = MusicUtil.convertToActualDataType(colType, indValue);
257 primaryKey = primaryKey + indValue;
258 rowSpec = rowSpec + keyName + "= ? ";
259 queryObject.addValue(formattedValue);
260 if (counter != rowParams.size() - 1)
261 rowSpec = rowSpec + " AND ";
262 counter = counter + 1;
266 String ttl = insObj.getTtl();
267 String timestamp = insObj.getTimestamp();
269 if ((ttl != null) && (timestamp != null)) {
271 logger.info(EELFLoggerDelegate.applicationLogger,"both there");
272 queryObject.appendQueryString(" USING TTL ? AND TIMESTAMP ?");
273 queryObject.addValue(Integer.parseInt(ttl));
274 queryObject.addValue(Long.parseLong(timestamp));
277 if ((ttl != null) && (timestamp == null)) {
278 logger.info(EELFLoggerDelegate.applicationLogger,"ONLY TTL there");
279 queryObject.appendQueryString(" USING TTL ?");
280 queryObject.addValue(Integer.parseInt(ttl));
283 if ((ttl == null) && (timestamp != null)) {
284 logger.info(EELFLoggerDelegate.applicationLogger,"ONLY timestamp there");
285 queryObject.appendQueryString(" USING TIMESTAMP ?");
286 queryObject.addValue(Long.parseLong(timestamp));
288 queryObject.appendQueryString(" SET " + fieldValueString + " WHERE " + rowSpec + ";");
290 long jsonParseCompletionTime = System.currentTimeMillis();
292 boolean operationResult = true;
293 MusicCore.getDSHandle().executePut(queryObject, insObj.getConsistencyInfo().get("type"));
295 long actualUpdateCompletionTime = System.currentTimeMillis();
297 long endTime = System.currentTimeMillis();
299 String timingString = "Time taken in ms for Cassandra " + consistency + " update-"
300 + operationId + ":" + "|total operation time:" + (endTime - startTime)
301 + "|json parsing time:" + (jsonParseCompletionTime - startTime)
302 + "|update time:" + (actualUpdateCompletionTime - jsonParseCompletionTime)
304 logger.info(EELFLoggerDelegate.applicationLogger,timingString);
306 return operationResult;