2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 AT&T Intellectual Property
6 * ===================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ============LICENSE_END=============================================
20 * ====================================================================
23 package org.onap.music.rest;
25 import java.util.List;
27 import java.util.UUID;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.POST;
31 import javax.ws.rs.PUT;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.MultivaluedMap;
38 import javax.ws.rs.core.UriInfo;
39 import org.onap.music.datastore.jsonobjects.JsonInsert;
40 import org.onap.music.eelf.logging.EELFLoggerDelegate;
41 import org.onap.music.main.MusicCore;
42 import org.onap.music.main.MusicUtil;
43 import org.onap.music.main.ResultType;
44 import org.onap.music.main.ReturnType;
45 import org.onap.music.service.impl.MusicZKCore;
46 import org.onap.music.datastore.MusicDataStoreHandle;
47 import org.onap.music.datastore.PreparedQueryObject;
48 import com.datastax.driver.core.DataType;
49 import com.datastax.driver.core.TableMetadata;
50 import io.swagger.annotations.Api;
53 * These are functions created purely for benchmarking purposes. Commented out Swagger - This should
57 @Path("/v{version: [0-9]+}/benchmarks/")
58 @Api(value = "Benchmark API", hidden = true)
59 public class RestMusicBmAPI {
61 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicBmAPI.class);
62 public static final String UPDATE_CONST=" update-";
71 @Path("/purezk/{name}")
72 @Consumes(MediaType.APPLICATION_JSON)
73 public void pureZkCreate(@PathParam("name") String nodeName) throws Exception {
74 MusicZKCore.pureZkCreate("/" + nodeName);
85 @Path("/purezk/{name}")
86 @Consumes(MediaType.APPLICATION_JSON)
87 public void pureZkUpdate(JsonInsert insObj, @PathParam("name") String nodeName)
89 logger.info(EELFLoggerDelegate.applicationLogger,"--------------Zk normal update-------------------------");
90 long start = System.currentTimeMillis();
91 MusicZKCore.pureZkWrite(nodeName, insObj.serialize());
92 long end = System.currentTimeMillis();
93 logger.info(EELFLoggerDelegate.applicationLogger,"Total time taken for Zk normal update:" + (end - start) + " ms");
103 @Path("/purezk/{name}")
104 @Consumes(MediaType.TEXT_PLAIN)
105 public byte[] pureZkGet(@PathParam("name") String nodeName) throws Exception {
106 return MusicZKCore.pureZkRead(nodeName);
117 @Path("/purezk/atomic/{lockname}/{name}")
118 @Consumes(MediaType.APPLICATION_JSON)
119 public void pureZkAtomicPut(JsonInsert updateObj, @PathParam("lockname") String lockname,
120 @PathParam("name") String nodeName) throws Exception {
121 long startTime = System.currentTimeMillis();
122 String operationId = UUID.randomUUID().toString();// just for debugging purposes.
123 String consistency = updateObj.getConsistencyInfo().get("type");
125 logger.info(EELFLoggerDelegate.applicationLogger,"--------------Zookeeper " + consistency + UPDATE_CONST + operationId
126 + "-------------------------");
128 byte[] data = updateObj.serialize();
129 long jsonParseCompletionTime = System.currentTimeMillis();
131 String lockId = MusicCore.createLockReference(lockname);
133 long lockCreationTime = System.currentTimeMillis();
135 long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
136 ReturnType lockAcqResult = MusicCore.acquireLockWithLease(lockname, lockId, leasePeriod);
137 long lockAcqTime = System.currentTimeMillis();
139 long lockReleaseTime = 0;
141 if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
142 logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId);
143 MusicZKCore.pureZkWrite(lockname, data);
144 zkPutTime = System.currentTimeMillis();
145 boolean voluntaryRelease = true;
147 if (("atomic").equals(consistency))
148 MusicCore.releaseLock(lockId, voluntaryRelease);
149 else if (("atomic_delete_lock").equals(consistency))
150 MusicCore.deleteLock(lockname);
152 if (consistency.equals("atomic"))
153 MusicCore.releaseLock(lockId, voluntaryRelease);
154 else if (consistency.equals("atomic_delete_lock"))
155 MusicCore.deleteLock(lockname);
156 lockReleaseTime = System.currentTimeMillis();
158 MusicCore.destroyLockRef(lockId);
161 long actualUpdateCompletionTime = System.currentTimeMillis();
164 long endTime = System.currentTimeMillis();
166 String lockingInfo = "|lock creation time:" + (lockCreationTime - jsonParseCompletionTime)
167 + "|lock accquire time:" + (lockAcqTime - lockCreationTime)
168 + "|zk put time:" + (zkPutTime - lockAcqTime);
170 if ("atomic".equals(consistency))
171 lockingInfo = lockingInfo + "|lock release time:" + (lockReleaseTime - zkPutTime) + "|";
172 else if ("atomic_delete_lock".equals(consistency))
173 lockingInfo = lockingInfo + "|lock delete time:" + (lockReleaseTime - zkPutTime) + "|";
175 String timingString = "Time taken in ms for Zookeeper " + consistency + UPDATE_CONST
176 + operationId + ":" + "|total operation time:" + (endTime - startTime)
177 + "|json parsing time:" + (jsonParseCompletionTime - startTime)
178 + "|update time:" + (actualUpdateCompletionTime - jsonParseCompletionTime)
181 logger.info(EELFLoggerDelegate.applicationLogger,timingString);
192 @Path("/purezk/atomic/{lockname}/{name}")
193 @Consumes(MediaType.APPLICATION_JSON)
194 public void pureZkAtomicGet(JsonInsert insObj, @PathParam("lockname") String lockName,
195 @PathParam("name") String nodeName) throws Exception {
196 logger.info("--------------Zk atomic read-------------------------");
197 long start = System.currentTimeMillis();
198 String lockId = MusicCore.createLockReference(lockName);
199 long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
200 ReturnType lockAcqResult = MusicCore.acquireLockWithLease(lockName, lockId, leasePeriod);
201 if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
202 logger.info("acquired lock with id " + lockId);
203 MusicZKCore.pureZkRead(nodeName);
204 boolean voluntaryRelease = true;
205 MusicCore.releaseLock(lockId, voluntaryRelease);
207 MusicCore.destroyLockRef(lockId);
210 long end = System.currentTimeMillis();
211 logger.info(EELFLoggerDelegate.applicationLogger,"Total time taken for Zk atomic read:" + (end - start) + " ms");
216 * doing an update directly to cassa but through the rest api
226 @Path("/cassa/keyspaces/{keyspace}/tables/{tablename}/rows")
227 @Consumes(MediaType.APPLICATION_JSON)
228 @Produces(MediaType.APPLICATION_JSON)
229 public boolean updateTableCassa(JsonInsert insObj, @PathParam("keyspace") String keyspace,
230 @PathParam("tablename") String tablename, @Context UriInfo info)
232 long startTime = System.currentTimeMillis();
233 String operationId = UUID.randomUUID().toString();// just for debugging purposes.
234 String consistency = insObj.getConsistencyInfo().get("type");
235 logger.info(EELFLoggerDelegate.applicationLogger,"--------------Cassandra " + consistency + UPDATE_CONST + operationId
236 + "-------------------------");
237 PreparedQueryObject queryObject = new PreparedQueryObject();
238 Map<String, Object> valuesMap = insObj.getValues();
239 TableMetadata tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename);
240 String vectorTs = "'" + Thread.currentThread().getId() + System.currentTimeMillis() + "'";
241 String fieldValueString = "vector_ts= ? ,";
242 queryObject.addValue(vectorTs);
245 for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
246 Object valueObj = entry.getValue();
247 DataType colType = tableInfo.getColumn(entry.getKey()).getType();
248 Object valueString = MusicUtil.convertToActualDataType(colType, valueObj);
249 fieldValueString = fieldValueString + entry.getKey() + "= ?";
250 queryObject.addValue(valueString);
251 if (counter != valuesMap.size() - 1)
252 fieldValueString = fieldValueString + ",";
253 counter = counter + 1;
256 // get the row specifier
259 queryObject.appendQueryString("UPDATE " + keyspace + "." + tablename + " ");
260 MultivaluedMap<String, String> rowParams = info.getQueryParameters();
261 String primaryKey = "";
262 for (MultivaluedMap.Entry<String, List<String>> entry : rowParams.entrySet()) {
263 String keyName = entry.getKey();
264 List<String> valueList = entry.getValue();
265 String indValue = valueList.get(0);
266 DataType colType = tableInfo.getColumn(entry.getKey()).getType();
267 Object formattedValue = MusicUtil.convertToActualDataType(colType, indValue);
268 primaryKey = primaryKey + indValue;
269 rowSpec = rowSpec + keyName + "= ? ";
270 queryObject.addValue(formattedValue);
271 if (counter != rowParams.size() - 1)
272 rowSpec = rowSpec + " AND ";
273 counter = counter + 1;
277 String ttl = insObj.getTtl();
278 String timestamp = insObj.getTimestamp();
280 if ((ttl != null) && (timestamp != null)) {
282 logger.info(EELFLoggerDelegate.applicationLogger,"both there");
283 queryObject.appendQueryString(" USING TTL ? AND TIMESTAMP ?");
284 queryObject.addValue(Integer.parseInt(ttl));
285 queryObject.addValue(Long.parseLong(timestamp));
288 if ((ttl != null) && (timestamp == null)) {
289 logger.info(EELFLoggerDelegate.applicationLogger,"ONLY TTL there");
290 queryObject.appendQueryString(" USING TTL ?");
291 queryObject.addValue(Integer.parseInt(ttl));
294 if ((ttl == null) && (timestamp != null)) {
295 logger.info(EELFLoggerDelegate.applicationLogger,"ONLY timestamp there");
296 queryObject.appendQueryString(" USING TIMESTAMP ?");
297 queryObject.addValue(Long.parseLong(timestamp));
299 queryObject.appendQueryString(" SET " + fieldValueString + " WHERE " + rowSpec + ";");
301 long jsonParseCompletionTime = System.currentTimeMillis();
303 boolean operationResult = true;
304 MusicDataStoreHandle.getDSHandle().executePut(queryObject, insObj.getConsistencyInfo().get("type"));
306 long actualUpdateCompletionTime = System.currentTimeMillis();
308 long endTime = System.currentTimeMillis();
310 String timingString = "Time taken in ms for Cassandra " + consistency + UPDATE_CONST
311 + operationId + ":" + "|total operation time:" + (endTime - startTime)
312 + "|json parsing time:" + (jsonParseCompletionTime - startTime)
313 + "|update time:" + (actualUpdateCompletionTime - jsonParseCompletionTime)
315 logger.info(EELFLoggerDelegate.applicationLogger,timingString);
317 return operationResult;