Search in sources :

Example 1 with Echo

use of com.xrtb.commands.Echo in project XRTB by benmfaul.

the class AddShutdownHook method getStatus.

/**
	 * Returns the status of this server.
	 * 
	 * @return Map. A map representation of this status.
	 */
public static Echo getStatus() {
    setSummaryStats();
    Echo e = new Echo();
    e.from = Configuration.getInstance().instanceName;
    e.percentage = percentage.intValue();
    e.stopped = stopped;
    e.request = request;
    e.bid = bid;
    e.win = win;
    e.nobid = nobid;
    e.error = error;
    e.handled = handled;
    e.unknown = unknown;
    e.clicks = clicks;
    e.pixel = pixels;
    e.fraud = fraud;
    e.adspend = adspend;
    e.loglevel = Configuration.getInstance().logLevel;
    e.qps = qps;
    e.campaigns = Configuration.getInstance().campaignsList;
    e.avgx = avgx;
    e.exchanges = BidRequest.getExchangeCounts();
    e.timestamp = System.currentTimeMillis();
    if (CampaignProcessor.probe != null) {
        e.cperform = CampaignProcessor.probe.getMap();
    }
    String perf = Performance.getCpuPerfAsString();
    int threads = Performance.getThreadCount();
    String pf = Performance.getPercFreeDisk();
    String mem = Performance.getMemoryUsed();
    e.threads = threads;
    e.memory = mem;
    e.freeDisk = pf;
    e.cpu = perf;
    return e;
}
Also used : Echo(com.xrtb.commands.Echo)

Example 2 with Echo

use of com.xrtb.commands.Echo in project XRTB by benmfaul.

the class CommandLoop method onMessage.

/**
	 * On a message from 0MQ, handle the command.
	 * 
	 * @param arg0
	 *            . String - the channel of this message.
	 */
@Override
public void onMessage(String arg0, BasicCommand item) {
    try {
        if (item.to != null && (item.to.equals("*") == false)) {
            boolean mine = Configuration.instanceName.matches(item.to);
            if (item.to.equals("") == false && !mine) {
                Controller.getInstance().sendLog(5, "Controller:onMessage:" + item, "Message was not for me: " + item);
                return;
            }
        }
    } catch (Exception error) {
        try {
            Echo m = new Echo();
            m.from = Configuration.instanceName;
            m.to = item.from;
            m.id = item.id;
            m.status = "error";
            m.msg = error.toString();
            Controller.getInstance().responseQueue.add(m);
            Controller.getInstance().sendLog(1, "Controller:onMessage:" + item, "Error: " + error.toString());
            return;
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }
    try {
        Runnable task = null;
        Thread thread;
        switch(item.cmd) {
            case Controller.GET_PRICE:
                task = () -> {
                    try {
                        Controller.getInstance().getPrice(item);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                };
                thread = new Thread(task);
                thread.start();
                break;
            case Controller.SET_PRICE:
                task = () -> {
                    try {
                        Controller.getInstance().setPrice((SetPrice) item);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                };
                thread = new Thread(task);
                thread.start();
                break;
            case Controller.ADD_CAMPAIGN:
                task = () -> {
                    try {
                        Controller.getInstance().addCampaign(item);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                };
                thread = new Thread(task);
                thread.start();
                break;
            case Controller.CONFIGURE_AWS_OBJECT:
                task = () -> {
                    try {
                        Controller.getInstance().configureAwsObject(item);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                };
                thread = new Thread(task);
                thread.start();
                break;
            case Controller.ADD_CAMPAIGNS_LIST:
                task = () -> {
                    try {
                        Controller.getInstance().addCampaignsList(item);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                };
                thread = new Thread(task);
                thread.start();
                break;
            case Controller.DEL_CAMPAIGN:
                task = () -> {
                    try {
                        Controller.getInstance().deleteCampaign(item);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                };
                thread = new Thread(task);
                thread.start();
                break;
            case Controller.DELETE_USER:
                task = () -> {
                    try {
                        Controller.getInstance().deleteUser(item);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                };
                thread = new Thread(task);
                thread.start();
                break;
            case Controller.STOP_BIDDER:
                Controller.getInstance().stopBidder(item);
                break;
            case Controller.START_BIDDER:
                Controller.getInstance().startBidder(item);
                break;
            case Controller.ECHO:
                Controller.getInstance().echo(item);
                break;
            case Controller.SETLOGLEVEL:
                Controller.getInstance().setLogLevel(item);
                break;
            case Controller.DELETE_CREATIVE:
                Controller.getInstance().deleteCreative((DeleteCreative) item);
                break;
            default:
                Controller.getInstance().notHandled(item);
        }
    } catch (Exception error) {
        try {
            item.msg = error.toString();
            item.to = item.from;
            item.from = Configuration.getInstance().instanceName;
            item.status = "error";
            Controller.getInstance().responseQueue.add(item);
            error.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        error.printStackTrace();
    }
}
Also used : Echo(com.xrtb.commands.Echo)

Example 3 with Echo

use of com.xrtb.commands.Echo in project XRTB by benmfaul.

the class TestZZZRedis method testEcho.

/**
	 * Test the echo/status message
	 * 
	 * @throws Exception
	 *             if the Controller is not complete.
	 */
@Test
public void testEcho() throws Exception {
    Thread.sleep(1000);
    for (int i = 0; i < 10; i++) {
        Echo e = new Echo();
        String str = e.toString();
        e.id = "ECHO-ID";
        rcv = null;
        latch = new CountDownLatch(1);
        commands.add(e);
        latch.await(5, TimeUnit.SECONDS);
        assertTrue(rcv.cmd == 5);
    }
}
Also used : Echo(com.xrtb.commands.Echo) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with Echo

use of com.xrtb.commands.Echo in project XRTB by benmfaul.

the class TestZZZRedis method testSetLogLevel.

/**
	 * Test the echo/status message
	 * 
	 * @throws Exception
	 *             if the Controller is not complete.
	 */
@Test
public void testSetLogLevel() throws Exception {
    LogLevel e = new LogLevel("*", "-3");
    e.id = "SETLOG-ID";
    rcv = null;
    latch = new CountDownLatch(1);
    commands.add(e);
    latch.await(5, TimeUnit.SECONDS);
    Echo echo = (Echo) rcv;
    // System.out.println(echo.toString());
    assertEquals(echo.loglevel, -3);
}
Also used : Echo(com.xrtb.commands.Echo) CountDownLatch(java.util.concurrent.CountDownLatch) LogLevel(com.xrtb.commands.LogLevel) Test(org.junit.Test)

Example 5 with Echo

use of com.xrtb.commands.Echo in project XRTB by benmfaul.

the class AddShutdownHook method startPeridocLogger.

/**
	 * Quickie tasks for periodic logging
	 */
void startPeridocLogger() throws Exception {
    if (Configuration.getInstance().cacheHost != null) {
        node = new MyNameNode(Configuration.getInstance().cacheHost, Configuration.getInstance().cachePort);
        Runnable redisupdater = () -> {
            try {
                while (true) {
                    Echo e = getStatus();
                    Controller.getInstance().setMemberStatus(e);
                    Controller.getInstance().updateStatusZooKeeper(e.toJson());
                    Controller.getInstance().reportNoBidReasons();
                    CampaignProcessor.probe.reset();
                    Thread.sleep(ZOOKEEPER_UPDATE);
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        };
        Thread nthread = new Thread(redisupdater);
        nthread.start();
    }
    ////////////////////
    Runnable task = () -> {
        long count = 0;
        while (true) {
            try {
                // RTBServer.paused = true; // for a short time, send
                // no-bids, this way any
                // queues needing to drain
                // have a chance to do so
                avgBidTime = totalBidTime.get();
                double davgBidTime = avgBidTime;
                double window = bidCountWindow.get();
                if (window == 0)
                    window = 1;
                davgBidTime /= window;
                String sqps = String.format("%.2f", qps);
                String savgbidtime = String.format("%.2f", davgBidTime);
                long a = ForensiqClient.forensiqXtime.get();
                long b = ForensiqClient.forensiqCount.get();
                ForensiqClient.forensiqXtime.set(0);
                ForensiqClient.forensiqCount.set(0);
                totalBidTime.set(0);
                bidCountWindow.set(0);
                server.getThreadPool().isLowOnThreads();
                if (b == 0)
                    b = 1;
                long avgForensiq = a / b;
                String perf = Performance.getCpuPerfAsString();
                int threads = Performance.getThreadCount();
                String pf = Performance.getPercFreeDisk();
                String mem = Performance.getMemoryUsed();
                long of = Performance.getOpenFileDescriptorCount();
                List exchangeCounts = BidRequest.getExchangeCounts();
                String msg = "openfiles=" + of + ", cpu=" + perf + "%, mem=" + mem + ", freedsk=" + pf + "%, threads=" + threads + ", low-on-threads= " + server.getThreadPool().isLowOnThreads() + ", qps=" + sqps + ", avgBidTime=" + savgbidtime + "ms, avgForensiq= " + avgForensiq + "ms, total=" + handled + ", requests=" + request + ", bids=" + bid + ", nobids=" + nobid + ", fraud=" + fraud + ", wins=" + win + ", pixels=" + pixels + ", clicks=" + clicks + ", exchanges= " + exchangeCounts + ", stopped=" + stopped + ", campaigns=" + Configuration.getInstance().campaignsList.size();
                Map m = new HashMap();
                m.put("timestamp", System.currentTimeMillis());
                m.put("hostname", Configuration.getInstance().instanceName);
                m.put("openfiles", of);
                m.put("cpu", Double.parseDouble(perf));
                m.put("bp", Controller.getInstance().getBackPressure());
                String[] parts = mem.split("M");
                m.put("memused", Double.parseDouble(parts[0]));
                parts[1] = parts[1].substring(1, parts[1].length() - 2);
                parts[1] = parts[1].replaceAll("\\(", "");
                double percmemused = Double.parseDouble(parts[1]);
                m.put("percmemused", percmemused);
                m.put("freedisk", Double.parseDouble(pf));
                m.put("threads", threads);
                m.put("qps", qps);
                m.put("avgbidtime", Double.parseDouble(savgbidtime));
                m.put("handled", handled);
                m.put("requests", request);
                m.put("nobid", nobid);
                m.put("fraud", fraud);
                m.put("wins", win);
                m.put("pixels", pixels);
                m.put("clicks", clicks);
                m.put("stopped", stopped);
                m.put("bids", bid);
                m.put("exchanges", exchangeCounts);
                m.put("campaigns", Configuration.getInstance().campaignsList.size());
                if (CampaignProcessor.probe != null) {
                    // System.out.println("=======> REPORT: " +
                    // CampaignProcessor.probe.report());
                    m.put("cperform", CampaignProcessor.probe.getMap());
                }
                Controller.getInstance().sendStats(m);
                Controller.getInstance().sendLog(1, "Heartbeat", msg);
                CampaignSelector.adjustHighWaterMark();
                if (percmemused >= 94) {
                    Controller.getInstance().sendLog(1, "Memory Overusage", "Memory Usage Exceeded, Exiting");
                    Controller.getInstance().sendShutdown();
                    System.exit(1);
                }
                Thread.sleep(PERIODIC_UPDATE_TIME);
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        }
    };
    Thread thread = new Thread(task);
    thread.start();
}
Also used : Echo(com.xrtb.commands.Echo) HashMap(java.util.HashMap) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

Echo (com.xrtb.commands.Echo)6 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 LogLevel (com.xrtb.commands.LogLevel)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ServletException (javax.servlet.ServletException)1