Search in sources :

Example 6 with Point

use of com.srotya.sidewinder.core.rpc.Point in project sidewinder by srotya.

the class HTTPDataPointDecoder method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    try {
        if (ResourceMonitor.getInstance().isReject()) {
            logger.warning("Write rejected, insufficient memory");
            if (writeResponse(request, ctx)) {
                ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
            }
            return;
        }
        if (msg instanceof HttpRequest) {
            HttpRequest request = this.request = (HttpRequest) msg;
            if (HttpUtil.is100ContinueExpected(request)) {
                send100Continue(ctx);
            }
            QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.uri());
            path = queryStringDecoder.path();
            Map<String, List<String>> params = queryStringDecoder.parameters();
            if (!params.isEmpty()) {
                for (Entry<String, List<String>> p : params.entrySet()) {
                    String key = p.getKey();
                    if (key.equalsIgnoreCase("db")) {
                        dbName = p.getValue().get(0);
                    }
                }
            }
            if (path != null && path.contains("query")) {
                Gson gson = new Gson();
                JsonObject obj = new JsonObject();
                JsonArray ary = new JsonArray();
                ary.add(new JsonObject());
                obj.add("results", ary);
                responseString.append(gson.toJson(obj));
            }
        }
        if (msg instanceof HttpContent) {
            HttpContent httpContent = (HttpContent) msg;
            ByteBuf byteBuf = httpContent.content();
            if (byteBuf.isReadable()) {
                requestBuffer.append(byteBuf.toString(CharsetUtil.UTF_8));
            }
            if (msg instanceof LastHttpContent) {
                if (dbName == null) {
                    responseString.append("Invalid database null");
                    logger.severe("Invalid database null");
                } else {
                    String payload = requestBuffer.toString();
                    logger.fine("Request:" + payload);
                    List<Point> dps = InfluxDecoder.pointsFromString(dbName, payload);
                    meter.inc(dps.size());
                    for (Point dp : dps) {
                        try {
                            engine.writeDataPoint(dp);
                            logger.fine("Accepted:" + dp + "\t" + new Date(dp.getTimestamp()));
                        } catch (IOException e) {
                            logger.fine("Dropped:" + dp + "\t" + e.getMessage());
                            responseString.append("Dropped:" + dp);
                        }
                    }
                }
                if (writeResponse(request, ctx)) {
                    ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) Point(com.srotya.sidewinder.core.rpc.Point) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) Date(java.util.Date) IOException(java.io.IOException) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) JsonArray(com.google.gson.JsonArray) List(java.util.List) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 7 with Point

use of com.srotya.sidewinder.core.rpc.Point in project sidewinder by srotya.

the class InfluxApi method insertData.

@POST
@Consumes({ MediaType.TEXT_PLAIN })
public void insertData(@QueryParam("db") String dbName, String payload) {
    if (payload == null) {
        throw new BadRequestException("Empty request no acceptable");
    }
    List<Point> dps = InfluxDecoder.pointsFromString(dbName, payload);
    if (dps.isEmpty()) {
        throw new BadRequestException("Empty request no acceptable");
    }
    meter.mark(dps.size());
    for (Point dp : dps) {
        try {
            storageEngine.writeDataPoint(dp);
        } catch (IOException e) {
            throw new BadRequestException(e);
        }
    }
}
Also used : BadRequestException(javax.ws.rs.BadRequestException) Point(com.srotya.sidewinder.core.rpc.Point) IOException(java.io.IOException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 8 with Point

use of com.srotya.sidewinder.core.rpc.Point in project sidewinder by srotya.

the class TestMemStorageEngine method testWritePerformance.

public void testWritePerformance() throws Exception {
    final MemStorageEngine engine = new MemStorageEngine();
    engine.configure(new HashMap<>(), bgTasks);
    long timeMillis = System.currentTimeMillis();
    int tcount = 12;
    ExecutorService es = Executors.newCachedThreadPool();
    int count = 1000000;
    final int modulator = 100;
    final AtomicInteger rejects = new AtomicInteger(0);
    for (int k = 0; k < tcount; k++) {
        final int j = k;
        es.submit(new Thread() {

            @Override
            public void run() {
                long ts = System.currentTimeMillis();
                for (int i = 0; i < count; i++) {
                    if (isInterrupted()) {
                        break;
                    }
                    try {
                        Point dp = MiscUtils.buildDataPoint("test" + j, "cpu" + (i % modulator), "value", Arrays.asList("test2"), ts + i, i * 1.1);
                        engine.writeDataPoint(dp);
                    } catch (IOException e) {
                        rejects.incrementAndGet();
                    }
                }
            }
        });
    }
    es.shutdown();
    es.awaitTermination(120, TimeUnit.SECONDS);
    int backOff = 30;
    while (!es.isTerminated()) {
        backOff = backOff * 2;
        Thread.sleep(backOff);
    }
    System.out.println("Write throughput direct " + tcount + "x" + count + ":" + (System.currentTimeMillis() - timeMillis) + "ms with " + rejects.get() + " rejects using " + tcount + "\nWriting " + tcount + " each with " + (modulator) + " measurements");
    assertEquals(tcount, engine.getDatabases().size());
    for (int i = 0; i < tcount; i++) {
        String dbName = "test" + i;
        Set<String> allMeasurementsForDb = engine.getAllMeasurementsForDb(dbName);
        assertEquals(modulator, allMeasurementsForDb.size());
        for (String measurementName : allMeasurementsForDb) {
            List<Series> queryDataPoints = engine.queryDataPoints(dbName, measurementName, "value", timeMillis - (3600_000 * 2000), timeMillis + (3600_000 * 2000), null);
            assertEquals(1, queryDataPoints.size());
            assertEquals(count / modulator, queryDataPoints.iterator().next().getDataPoints().size());
        }
    }
}
Also used : DataPoint(com.srotya.sidewinder.core.storage.DataPoint) Point(com.srotya.sidewinder.core.rpc.Point) IOException(java.io.IOException) DataPoint(com.srotya.sidewinder.core.storage.DataPoint) Point(com.srotya.sidewinder.core.rpc.Point) TimeSeries(com.srotya.sidewinder.core.storage.TimeSeries) Series(com.srotya.sidewinder.core.storage.Series) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService)

Example 9 with Point

use of com.srotya.sidewinder.core.rpc.Point in project sidewinder by srotya.

the class TestDiskStorageEngine method testConcurrentOperations.

@Test
public void testConcurrentOperations() throws Exception {
    MiscUtils.delete(new File("target/dst-6/data"));
    final StorageEngine engine = new DiskStorageEngine();
    Map<String, String> conf = new HashMap<>();
    conf.put("data.dir", "target/dst-6/data");
    conf.put("index.dir", "target/dst-6/index");
    engine.configure(conf, bgTasks);
    final long ts = System.currentTimeMillis();
    ExecutorService es = Executors.newFixedThreadPool(2, new BackgrounThreadFactory("wr1"));
    String measurementName = "mmm2";
    String valueFieldName = "v1";
    String dbName = "db9";
    String tag = "h=1";
    for (int k = 0; k < 2; k++) {
        final int p = k;
        es.submit(() -> {
            long t = ts + p;
            for (int i = 0; i < 100; i++) {
                Point dp = MiscUtils.buildDataPoint(dbName, measurementName, valueFieldName, Arrays.asList(tag), t + i * 1000, i);
                try {
                    engine.writeDataPoint(dp);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
            System.err.println("Completed writes:" + 100 + " data points");
        });
    }
    es.shutdown();
    es.awaitTermination(100, TimeUnit.SECONDS);
    assertEquals(1, engine.getAllMeasurementsForDb(dbName).size());
    assertEquals(1, engine.getMeasurementMap().size());
    try {
        TimeSeries timeSeries = engine.getTimeSeries(dbName, measurementName, valueFieldName, Arrays.asList(tag));
        assertNotNull(timeSeries);
    } catch (ItemNotFoundException e) {
        fail("Time series must exist");
    }
    List<Series> queryDataPoints = engine.queryDataPoints(dbName, measurementName, valueFieldName, ts, ts + 220 * 1000, null);
    assertEquals(1, queryDataPoints.size());
    Series next = queryDataPoints.iterator().next();
    assertEquals(200, next.getDataPoints().size());
}
Also used : BackgrounThreadFactory(com.srotya.sidewinder.core.utils.BackgrounThreadFactory) TimeSeries(com.srotya.sidewinder.core.storage.TimeSeries) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DataPoint(com.srotya.sidewinder.core.storage.DataPoint) Point(com.srotya.sidewinder.core.rpc.Point) StorageEngine(com.srotya.sidewinder.core.storage.StorageEngine) DataPoint(com.srotya.sidewinder.core.storage.DataPoint) Point(com.srotya.sidewinder.core.rpc.Point) ItemNotFoundException(com.srotya.sidewinder.core.storage.ItemNotFoundException) RejectException(com.srotya.sidewinder.core.storage.RejectException) IOException(java.io.IOException) TimeSeries(com.srotya.sidewinder.core.storage.TimeSeries) Series(com.srotya.sidewinder.core.storage.Series) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File) ItemNotFoundException(com.srotya.sidewinder.core.storage.ItemNotFoundException) Test(org.junit.Test)

Example 10 with Point

use of com.srotya.sidewinder.core.rpc.Point in project sidewinder by srotya.

the class TestInfluxDecoder method testFloatMeasurementValueWithoutTimestamp.

@Test
public void testFloatMeasurementValueWithoutTimestamp() {
    String testPoints = "cpu value=1.2";
    List<Point> dps = InfluxDecoder.pointsFromString("test", testPoints);
    assertEquals(1, dps.size());
    Point dp = dps.get(0);
    assertEquals("cpu", dp.getMeasurementName());
    assertEquals(0, dp.getTagsList().size());
    assertEquals(1.2, Double.longBitsToDouble(dp.getValue()), 0.01);
    long ts = System.currentTimeMillis();
    assertEquals(1, (dp.getTimestamp() / ts), 1);
}
Also used : Point(com.srotya.sidewinder.core.rpc.Point) Test(org.junit.Test)

Aggregations

Point (com.srotya.sidewinder.core.rpc.Point)12 Test (org.junit.Test)8 IOException (java.io.IOException)4 DataPoint (com.srotya.sidewinder.core.storage.DataPoint)2 Series (com.srotya.sidewinder.core.storage.Series)2 TimeSeries (com.srotya.sidewinder.core.storage.TimeSeries)2 ExecutorService (java.util.concurrent.ExecutorService)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 Builder (com.srotya.sidewinder.core.rpc.Point.Builder)1 ItemNotFoundException (com.srotya.sidewinder.core.storage.ItemNotFoundException)1 RejectException (com.srotya.sidewinder.core.storage.RejectException)1 StorageEngine (com.srotya.sidewinder.core.storage.StorageEngine)1 BackgrounThreadFactory (com.srotya.sidewinder.core.utils.BackgrounThreadFactory)1 ByteBuf (io.netty.buffer.ByteBuf)1 HttpContent (io.netty.handler.codec.http.HttpContent)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)1