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.apache.log4j.Logger;
35 import org.onap.music.datastore.jsonobjects.JsonInsert;
36 import org.onap.music.datastore.jsonobjects.JsonOnboard;
37 import org.onap.music.datastore.jsonobjects.JsonUpdate;
38 import org.onap.music.main.CachingUtil;
39 import org.onap.music.main.MusicCore;
40 import org.onap.music.main.MusicUtil;
41 import org.onap.music.main.ResultType;
42 import org.onap.music.main.ReturnType;
43 import com.att.eelf.configuration.EELFLogger;
44 import com.att.eelf.configuration.EELFManager;
45 import org.onap.music.datastore.PreparedQueryObject;
46 import com.datastax.driver.core.DataType;
47 import com.datastax.driver.core.ResultSet;
48 import com.datastax.driver.core.TableMetadata;
49 import io.swagger.annotations.Api;
50 import io.swagger.annotations.ApiOperation;
51 import io.swagger.annotations.ApiParam;
54 * These are functions created purely for benchmarking purposes. Commented out Swagger - This should
58 @Path("/v{version: [0-9]+}/benchmarks/")
59 @Api(value = "Benchmark API", hidden = true)
60 public class RestMusicBmAPI {
61 private static EELFLogger logger = EELFManager.getInstance().getLogger(RestMusicBmAPI.class);
71 @Path("/purezk/{name}")
72 @Consumes(MediaType.APPLICATION_JSON)
73 public void pureZkCreate(@PathParam("name") String nodeName) throws Exception {
74 MusicCore.pureZkCreate("/" + nodeName);
85 @Path("/purezk/{name}")
86 @Consumes(MediaType.APPLICATION_JSON)
87 public void pureZkUpdate(JsonInsert insObj, @PathParam("name") String nodeName)
89 logger.info("--------------Zk normal update-------------------------");
90 long start = System.currentTimeMillis();
91 MusicCore.pureZkWrite(nodeName, insObj.serialize());
92 long end = System.currentTimeMillis();
93 logger.info("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 MusicCore.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("--------------Zookeeper " + consistency + " update-" + 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();
138 long zkPutTime = 0, lockReleaseTime = 0;
140 if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
141 logger.info("acquired lock with id " + lockId);
142 MusicCore.pureZkWrite(lockname, data);
143 zkPutTime = System.currentTimeMillis();
144 boolean voluntaryRelease = true;
145 if (consistency.equals("atomic"))
146 MusicCore.releaseLock(lockId, voluntaryRelease);
147 else if (consistency.equals("atomic_delete_lock"))
148 MusicCore.deleteLock(lockname);
149 lockReleaseTime = System.currentTimeMillis();
151 MusicCore.destroyLockRef(lockId);
154 long actualUpdateCompletionTime = System.currentTimeMillis();
157 long endTime = System.currentTimeMillis();
159 String lockingInfo = "|lock creation time:" + (lockCreationTime - jsonParseCompletionTime)
160 + "|lock accquire time:" + (lockAcqTime - lockCreationTime)
161 + "|zk put time:" + (zkPutTime - lockAcqTime);
163 if (consistency.equals("atomic"))
164 lockingInfo = lockingInfo + "|lock release time:" + (lockReleaseTime - zkPutTime) + "|";
165 else if (consistency.equals("atomic_delete_lock"))
166 lockingInfo = lockingInfo + "|lock delete time:" + (lockReleaseTime - zkPutTime) + "|";
168 String timingString = "Time taken in ms for Zookeeper " + consistency + " update-"
169 + operationId + ":" + "|total operation time:" + (endTime - startTime)
170 + "|json parsing time:" + (jsonParseCompletionTime - startTime)
171 + "|update time:" + (actualUpdateCompletionTime - jsonParseCompletionTime)
174 logger.info(timingString);
185 @Path("/purezk/atomic/{lockname}/{name}")
186 @Consumes(MediaType.APPLICATION_JSON)
187 public void pureZkAtomicGet(JsonInsert insObj, @PathParam("lockname") String lockName,
188 @PathParam("name") String nodeName) throws Exception {
189 logger.info("--------------Zk atomic read-------------------------");
190 long start = System.currentTimeMillis();
191 String lockId = MusicCore.createLockReference(lockName);
192 long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
193 ReturnType lockAcqResult = MusicCore.acquireLockWithLease(lockName, lockId, leasePeriod);
194 if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
195 logger.info("acquired lock with id " + lockId);
196 MusicCore.pureZkRead(nodeName);
197 boolean voluntaryRelease = true;
198 MusicCore.releaseLock(lockId, voluntaryRelease);
200 MusicCore.destroyLockRef(lockId);
203 long end = System.currentTimeMillis();
204 logger.info("Total time taken for Zk atomic read:" + (end - start) + " ms");
209 * doing an update directly to cassa but through the rest api
219 @Path("/cassa/keyspaces/{keyspace}/tables/{tablename}/rows")
220 @Consumes(MediaType.APPLICATION_JSON)
221 @Produces(MediaType.APPLICATION_JSON)
222 public boolean updateTableCassa(JsonInsert insObj, @PathParam("keyspace") String keyspace,
223 @PathParam("tablename") String tablename, @Context UriInfo info)
225 long startTime = System.currentTimeMillis();
226 String operationId = UUID.randomUUID().toString();// just for debugging purposes.
227 String consistency = insObj.getConsistencyInfo().get("type");
228 logger.info("--------------Cassandra " + consistency + " update-" + operationId
229 + "-------------------------");
230 PreparedQueryObject queryObject = new PreparedQueryObject();
231 Map<String, Object> valuesMap = insObj.getValues();
232 TableMetadata tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename);
233 String vectorTs = "'" + Thread.currentThread().getId() + System.currentTimeMillis() + "'";
234 String fieldValueString = "vector_ts= ? ,";
235 queryObject.addValue(vectorTs);
238 for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
239 Object valueObj = entry.getValue();
240 DataType colType = tableInfo.getColumn(entry.getKey()).getType();
241 Object valueString = MusicUtil.convertToActualDataType(colType, valueObj);
242 fieldValueString = fieldValueString + entry.getKey() + "= ?";
243 queryObject.addValue(valueString);
244 if (counter != valuesMap.size() - 1)
245 fieldValueString = fieldValueString + ",";
246 counter = counter + 1;
249 // get the row specifier
252 queryObject.appendQueryString("UPDATE " + keyspace + "." + tablename + " ");
253 MultivaluedMap<String, String> rowParams = info.getQueryParameters();
254 String primaryKey = "";
255 for (MultivaluedMap.Entry<String, List<String>> entry : rowParams.entrySet()) {
256 String keyName = entry.getKey();
257 List<String> valueList = entry.getValue();
258 String indValue = valueList.get(0);
259 DataType colType = tableInfo.getColumn(entry.getKey()).getType();
260 Object formattedValue = MusicUtil.convertToActualDataType(colType, indValue);
261 primaryKey = primaryKey + indValue;
262 rowSpec = rowSpec + keyName + "= ? ";
263 queryObject.addValue(formattedValue);
264 if (counter != rowParams.size() - 1)
265 rowSpec = rowSpec + " AND ";
266 counter = counter + 1;
270 String ttl = insObj.getTtl();
271 String timestamp = insObj.getTimestamp();
273 if ((ttl != null) && (timestamp != null)) {
275 logger.info("both there");
276 queryObject.appendQueryString(" USING TTL ? AND TIMESTAMP ?");
277 queryObject.addValue(Integer.parseInt(ttl));
278 queryObject.addValue(Long.parseLong(timestamp));
281 if ((ttl != null) && (timestamp == null)) {
282 logger.info("ONLY TTL there");
283 queryObject.appendQueryString(" USING TTL ?");
284 queryObject.addValue(Integer.parseInt(ttl));
287 if ((ttl == null) && (timestamp != null)) {
288 logger.info("ONLY timestamp there");
289 queryObject.appendQueryString(" USING TIMESTAMP ?");
290 queryObject.addValue(Long.parseLong(timestamp));
292 queryObject.appendQueryString(" SET " + fieldValueString + " WHERE " + rowSpec + ";");
294 long jsonParseCompletionTime = System.currentTimeMillis();
296 boolean operationResult = true;
297 MusicCore.getDSHandle().executePut(queryObject, insObj.getConsistencyInfo().get("type"));
299 long actualUpdateCompletionTime = System.currentTimeMillis();
301 long endTime = System.currentTimeMillis();
303 String timingString = "Time taken in ms for Cassandra " + consistency + " update-"
304 + operationId + ":" + "|total operation time:" + (endTime - startTime)
305 + "|json parsing time:" + (jsonParseCompletionTime - startTime)
306 + "|update time:" + (actualUpdateCompletionTime - jsonParseCompletionTime)
308 logger.info(timingString);
310 return operationResult;