Search in sources :

Example 21 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class TopologyWebConsolePlugin method handleTopologyEvent.

/**
     * keep a truncated history of the log events for information purpose (to be shown in the webconsole)
     */
@Override
public void handleTopologyEvent(final TopologyEvent event) {
    if (event.getType() == Type.PROPERTIES_CHANGED) {
        this.currentView = event.getNewView();
        Set<InstanceDescription> newInstances = event.getNewView().getInstances();
        StringBuilder sb = new StringBuilder();
        for (Iterator<InstanceDescription> it = newInstances.iterator(); it.hasNext(); ) {
            final InstanceDescription newInstanceDescription = it.next();
            InstanceDescription oldInstanceDescription = findInstance(event.getOldView(), newInstanceDescription.getSlingId());
            if (oldInstanceDescription == null) {
                logger.error("handleTopologyEvent: got a property changed but did not find instance " + newInstanceDescription + " in oldview.. event=" + event);
                addEventLog(event.getType(), event.getType().toString());
                return;
            }
            Map<String, String> oldProps = oldInstanceDescription.getProperties();
            Map<String, String> newProps = newInstanceDescription.getProperties();
            StringBuilder diff = diff(oldProps, newProps);
            if (diff.length() > 0) {
                if (sb.length() != 0) {
                    sb.append(", ");
                }
                sb.append("on instance " + newInstanceDescription.getSlingId() + (newInstanceDescription.isLeader() ? " [isLeader]" : "") + ": " + diff);
            }
        }
        addEventLog(event.getType(), sb.toString());
    } else if (event.getType() == Type.TOPOLOGY_INIT) {
        this.currentView = event.getNewView();
        StringBuilder details = new StringBuilder();
        for (Iterator<InstanceDescription> it = event.getNewView().getInstances().iterator(); it.hasNext(); ) {
            InstanceDescription newInstance = it.next();
            if (details.length() != 0) {
                details.append(", ");
            }
            details.append(newInstance.getSlingId());
            if (newInstance.isLeader()) {
                details.append(" [isLeader]");
            }
        }
        addEventLog(event.getType(), "view: " + shortViewInfo(event.getNewView()) + ". " + details);
    } else if (event.getType() == Type.TOPOLOGY_CHANGING) {
        this.currentView = event.getOldView();
        addEventLog(event.getType(), "old view: " + shortViewInfo(event.getOldView()));
    } else {
        this.currentView = event.getNewView();
        if (event.getOldView() == null) {
            addEventLog(event.getType(), "new view: " + shortViewInfo(event.getNewView()));
        } else {
            StringBuilder details = new StringBuilder();
            for (Iterator<InstanceDescription> it = event.getNewView().getInstances().iterator(); it.hasNext(); ) {
                InstanceDescription newInstance = it.next();
                if (findInstance(event.getOldView(), newInstance.getSlingId()) == null) {
                    if (details.length() != 0) {
                        details.append(", ");
                    }
                    details.append(newInstance.getSlingId() + " joined");
                }
            }
            for (Iterator<InstanceDescription> it = event.getOldView().getInstances().iterator(); it.hasNext(); ) {
                InstanceDescription oldInstance = it.next();
                if (findInstance(event.getNewView(), oldInstance.getSlingId()) == null) {
                    if (details.length() != 0) {
                        details.append(", ");
                    }
                    details.append(oldInstance.getSlingId() + " left");
                }
            }
            final InstanceDescription li = event.getNewView().getLocalInstance();
            if (li != null) {
                ClusterView clusterView = li.getClusterView();
                if (clusterView != null) {
                    final InstanceDescription leader = clusterView.getLeader();
                    if (leader != null) {
                        if (details.length() != 0) {
                            details.append(", ");
                        }
                        details.append("[isLeader: " + leader.getSlingId() + "]");
                    }
                }
            }
            addEventLog(event.getType(), "old view: " + shortViewInfo(event.getOldView()) + ", new view: " + shortViewInfo(event.getNewView()) + ". " + details);
        }
    }
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) Iterator(java.util.Iterator) InstanceDescription(org.apache.sling.discovery.InstanceDescription)

Example 22 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class TopologyWebConsolePlugin method renderCluster.

/**
     * Render a particular cluster (into table rows)
     */
private void renderCluster(final PrintWriter pw, final ClusterView renderCluster, final ClusterView localCluster, final boolean odd, final boolean current) {
    final Collection<Announcement> announcements = announcementRegistry.listAnnouncementsInSameCluster(localCluster);
    for (Iterator<InstanceDescription> it = renderCluster.getInstances().iterator(); it.hasNext(); ) {
        final InstanceDescription instanceDescription = it.next();
        final boolean inLocalCluster = renderCluster == localCluster;
        Announcement parentAnnouncement = null;
        for (Iterator<Announcement> it2 = announcements.iterator(); it2.hasNext(); ) {
            Announcement announcement = it2.next();
            for (Iterator<InstanceDescription> it3 = announcement.listInstances().iterator(); it3.hasNext(); ) {
                InstanceDescription announcedInstance = it3.next();
                if (announcedInstance.getSlingId().equals(instanceDescription.getSlingId())) {
                    parentAnnouncement = announcement;
                    break;
                }
            }
        }
        final String oddEven = odd ? "odd" : "even";
        if (current && (inLocalCluster || (parentAnnouncement != null))) {
            pw.println("<tr class=\"" + oddEven + " ui-state-default\">");
        } else {
            pw.println("<tr class=\"" + oddEven + " ui-state-error\">");
        }
        final boolean isLocal = instanceDescription.isLocal();
        final String slingId = instanceDescription.getSlingId();
        pw.print("<td>");
        if (isLocal) {
            pw.print("<b>");
        }
        pw.print("<a href=\"");
        pw.print(this.getLabel());
        pw.print('/');
        pw.print(slingId);
        pw.print("\">");
        pw.print(slingId);
        pw.print("</a>");
        if (isLocal) {
            pw.print("</b>");
        }
        pw.println("</td>");
        pw.println("<td>" + (instanceDescription.getClusterView() == null ? "null" : instanceDescription.getClusterView().getId()) + "</td>");
        pw.println("<td>" + (isLocal ? "<b>true</b>" : "false") + "</td>");
        pw.println("<td>" + (instanceDescription.isLeader() ? "<b>true</b>" : "false") + "</td>");
        if (inLocalCluster) {
            pw.println("<td>local</td>");
            pw.println("<td>n/a</td>");
        } else {
            pw.println("<td>remote</td>");
            if (parentAnnouncement != null) {
                pw.println("<td>" + parentAnnouncement.getOwnerId() + "</td>");
            } else {
                pw.println("<td><b>(changing)</b></td>");
            }
        }
        pw.println("</tr>");
    }
}
Also used : Announcement(org.apache.sling.discovery.base.connectors.announcement.Announcement) CachedAnnouncement(org.apache.sling.discovery.base.connectors.announcement.CachedAnnouncement) InstanceDescription(org.apache.sling.discovery.InstanceDescription)

Example 23 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class TopologyWebConsolePlugin method renderProperties.

/**
     * Render the properties page of a particular instance
     */
private void renderProperties(final PrintWriter pw, final String contextPath, final String nodeId) {
    if (logger.isDebugEnabled()) {
        logger.debug("renderProperties: nodeId=" + nodeId);
    }
    final TopologyView tv = this.currentView;
    @SuppressWarnings("unchecked") Set<InstanceDescription> instances = (tv == null ? (Set<InstanceDescription>) Collections.EMPTY_SET : tv.findInstances(new InstanceFilter() {

        @Override
        public boolean accept(InstanceDescription instance) {
            String slingId = instance.getSlingId();
            if (logger.isDebugEnabled()) {
                logger.debug("renderProperties/picks: slingId={}", slingId);
            }
            return (slingId.equals(nodeId));
        }
    }));
    if (instances != null && instances.size() == 1) {
        InstanceDescription instance = instances.iterator().next();
        pw.println("Properties of " + instance.getSlingId() + ":<br/>");
        pw.println("<table class=\"adapters nicetable ui-widget tablesorter\">");
        pw.println("<thead>");
        pw.println("<tr>");
        pw.println("<th class=\"header ui-widget-header\">Key</th>");
        pw.println("<th class=\"header ui-widget-header\">Value</th>");
        pw.println("</tr>");
        pw.println("</thead>");
        pw.println("<tbody>");
        boolean odd = true;
        for (Iterator<Entry<String, String>> it = instance.getProperties().entrySet().iterator(); it.hasNext(); ) {
            Entry<String, String> entry = it.next();
            String oddEven = odd ? "odd" : "even";
            odd = !odd;
            pw.println("<tr class=\"" + oddEven + " ui-state-default\">");
            pw.println("<td>" + entry.getKey() + "</td>");
            pw.println("<td>" + entry.getValue() + "</td>");
            pw.println("</tr>");
        }
        pw.println("</tbody>");
        pw.println("</table>");
    }
}
Also used : InstanceFilter(org.apache.sling.discovery.InstanceFilter) Entry(java.util.Map.Entry) HashSet(java.util.HashSet) Set(java.util.Set) InstanceDescription(org.apache.sling.discovery.InstanceDescription) TopologyView(org.apache.sling.discovery.TopologyView)

Example 24 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class DummyTopologyView method equals.

@Override
public boolean equals(Object obj) {
    if (!(obj instanceof DummyTopologyView)) {
        return false;
    }
    final DummyTopologyView other = (DummyTopologyView) obj;
    if (this == other) {
        return true;
    }
    if ((id == null && other.id != null) || (other.id == null && id != null) || (id != null && !id.equals(other.id))) {
        return false;
    }
    if (this.instances.size() != other.instances.size()) {
        return false;
    }
    for (Iterator<InstanceDescription> it = instances.iterator(); it.hasNext(); ) {
        InstanceDescription instanceDescription = (InstanceDescription) it.next();
        boolean found = false;
        for (Iterator<?> it2 = other.instances.iterator(); it2.hasNext(); ) {
            InstanceDescription otherId = (InstanceDescription) it2.next();
            if (instanceDescription.equals(otherId)) {
                found = true;
                break;
            }
        }
        if (!found) {
            return false;
        }
    }
    return true;
}
Also used : InstanceDescription(org.apache.sling.discovery.InstanceDescription)

Example 25 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class WebConsolePlugin method doGet.

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse res) throws ServletException, IOException {
    final String msg = req.getParameter("message");
    final PrintWriter pw = res.getWriter();
    pw.println("<form method='POST' name='eventingcmd'>" + "<input type='hidden' name='action' value=''/>" + "<input type='hidden' name='queue' value=''/>" + "</form>");
    pw.println("<script type='text/javascript'>");
    pw.println("function eventingsubmit(action, queue) {" + " document.forms['eventingcmd'].action.value = action;" + " document.forms['eventingcmd'].queue.value = queue;" + " document.forms['eventingcmd'].submit();" + "} </script>");
    pw.printf("<p class='statline ui-state-highlight'>Apache Sling Job Handling%s%n</p>", msg != null ? " : " + ResponseUtil.escapeXml(msg) : "");
    pw.println("<div class='ui-widget-header ui-corner-top buttonGroup'>");
    pw.println("<span style='float: left; margin-left: 1em'>Apache Sling Job Handling: Overall Statistics</span>");
    this.printForm(pw, null, "Reset Stats", "reset");
    pw.println("</div>");
    pw.println("<table class='nicetable'><tbody>");
    String topics = this.jobConsumerManager.getTopics();
    if (topics == null) {
        topics = "";
    } else {
        final String[] allTopics = topics.split(",");
        final StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (final String t : allTopics) {
            if (first) {
                first = false;
            } else {
                sb.append("<br/>");
            }
            sb.append(ResponseUtil.escapeXml(t));
        }
        topics = sb.toString();
    }
    Statistics s = this.jobManager.getStatistics();
    pw.printf("<tr><td>Start Time</td><td>%s</td></tr>", formatDate(s.getStartTime()));
    pw.printf("<tr><td>Local topic consumers: </td><td>%s</td></tr>", topics);
    pw.printf("<tr><td>Last Activated</td><td>%s</td></tr>", formatDate(s.getLastActivatedJobTime()));
    pw.printf("<tr><td>Last Finished</td><td>%s</td></tr>", formatDate(s.getLastFinishedJobTime()));
    pw.printf("<tr><td>Queued Jobs</td><td>%s</td></tr>", s.getNumberOfQueuedJobs());
    pw.printf("<tr><td>Active Jobs</td><td>%s</td></tr>", s.getNumberOfActiveJobs());
    pw.printf("<tr><td>Jobs</td><td>%s</td></tr>", s.getNumberOfJobs());
    pw.printf("<tr><td>Finished Jobs</td><td>%s</td></tr>", s.getNumberOfFinishedJobs());
    pw.printf("<tr><td>Failed Jobs</td><td>%s</td></tr>", s.getNumberOfFailedJobs());
    pw.printf("<tr><td>Cancelled Jobs</td><td>%s</td></tr>", s.getNumberOfCancelledJobs());
    pw.printf("<tr><td>Processed Jobs</td><td>%s</td></tr>", s.getNumberOfProcessedJobs());
    pw.printf("<tr><td>Average Processing Time</td><td>%s</td></tr>", formatTime(s.getAverageProcessingTime()));
    pw.printf("<tr><td>Average Waiting Time</td><td>%s</td></tr>", formatTime(s.getAverageWaitingTime()));
    pw.println("</tbody></table>");
    pw.println("<br/>");
    pw.println("<table class='nicetable'><tbody>");
    pw.println("<tr><th colspan='2'>Topology Capabilities</th></tr>");
    final TopologyCapabilities cap = this.configuration.getTopologyCapabilities();
    if (cap == null) {
        pw.print("<tr><td colspan='2'>No topology information available !</td></tr>");
    } else {
        final Map<String, List<InstanceDescription>> instanceCaps = cap.getInstanceCapabilities();
        for (final Map.Entry<String, List<InstanceDescription>> entry : instanceCaps.entrySet()) {
            final StringBuilder sb = new StringBuilder();
            for (final InstanceDescription id : entry.getValue()) {
                if (sb.length() > 0) {
                    sb.append("<br/>");
                }
                if (id.isLocal()) {
                    sb.append("<b>local</b>");
                } else {
                    sb.append(ResponseUtil.escapeXml(id.getSlingId()));
                }
            }
            pw.printf("<tr><td>%s</td><td>%s</td></tr>", ResponseUtil.escapeXml(entry.getKey()), sb.toString());
        }
    }
    pw.println("</tbody></table>");
    pw.println("<br/>");
    pw.println("<p class='statline'>Scheduled Jobs</p>");
    pw.println("<table class='nicetable'><tbody>");
    final Collection<ScheduledJobInfo> infos = this.jobManager.getScheduledJobs();
    if (infos.size() == 0) {
        pw.print("<tr><td colspan='5'>No jobs currently scheduled.</td></tr>");
    } else {
        pw.println("<tr><th>Schedule</th><th>Job Topic</th><th>Schedules</th></tr>");
        int index = 1;
        for (final ScheduledJobInfo info : infos) {
            pw.printf("<tr><td><b>%s</b></td><td>%s</td><td>", String.valueOf(index), ResponseUtil.escapeXml(info.getJobTopic()));
            boolean first = true;
            for (final ScheduleInfo si : info.getSchedules()) {
                if (!first) {
                    pw.print("<br/>");
                }
                first = false;
                switch(si.getType()) {
                    case YEARLY:
                        pw.printf("YEARLY %s %s : %s:%s", si.getMonthOfYear(), si.getDayOfMonth(), si.getHourOfDay(), si.getMinuteOfHour());
                        break;
                    case MONTHLY:
                        pw.printf("MONTHLY %s : %s:%s", si.getDayOfMonth(), si.getHourOfDay(), si.getMinuteOfHour());
                        break;
                    case WEEKLY:
                        pw.printf("WEEKLY %s : %s:%s", si.getDayOfWeek(), si.getHourOfDay(), si.getMinuteOfHour());
                        break;
                    case DAILY:
                        pw.printf("DAILY %s:%s", si.getHourOfDay(), si.getMinuteOfHour());
                        break;
                    case HOURLY:
                        pw.printf("HOURLY %s", si.getMinuteOfHour());
                        break;
                    case CRON:
                        pw.printf("CRON %s", ResponseUtil.escapeXml(si.getExpression()));
                        break;
                    default:
                        pw.printf("AT %s", si.getAt());
                }
            }
            pw.print("</td></tr>");
            index++;
        }
    }
    pw.println("</tbody></table>");
    pw.println("<br/>");
    boolean isEmpty = true;
    for (final Queue q : this.jobManager.getQueues()) {
        isEmpty = false;
        final String queueName = q.getName();
        pw.println("<div class='ui-widget-header ui-corner-top buttonGroup'>");
        pw.printf("<span style='float: left; margin-left: 1em'>Active JobQueue: %s %s</span>", ResponseUtil.escapeXml(queueName), q.isSuspended() ? "(SUSPENDED)" : "");
        this.printForm(pw, queueName, "Reset Stats", "reset");
        if (q.isSuspended()) {
            this.printForm(pw, queueName, "Resume", "resume");
        } else {
            this.printForm(pw, queueName, "Suspend", "suspend");
        }
        this.printForm(pw, queueName, "Test", "test");
        this.printForm(pw, queueName, "Drop All", "dropall");
        pw.println("</div>");
        pw.println("<table class='nicetable'><tbody>");
        s = q.getStatistics();
        final QueueConfiguration c = q.getConfiguration();
        pw.println("<tr><th colspan='2'>Statistics</th><th colspan='2'>Configuration</th></tr>");
        pw.printf("<tr><td>Start Time</td><td>%s</td><td>Type</td><td>%s</td></tr>", formatDate(s.getStartTime()), formatType(c.getType()));
        pw.printf("<tr><td>Last Activated</td><td>%s</td><td>Topics</td><td>%s</td></tr>", formatDate(s.getLastActivatedJobTime()), formatArray(c.getTopics()));
        pw.printf("<tr><td>Last Finished</td><td>%s</td><td>Max Parallel</td><td>%s</td></tr>", formatDate(s.getLastFinishedJobTime()), c.getMaxParallel());
        pw.printf("<tr><td>Queued Jobs</td><td>%s</td><td>Max Retries</td><td>%s</td></tr>", s.getNumberOfQueuedJobs(), c.getMaxRetries());
        pw.printf("<tr><td>Active Jobs</td><td>%s</td><td>Retry Delay</td><td>%s ms</td></tr>", s.getNumberOfActiveJobs(), c.getRetryDelayInMs());
        pw.printf("<tr><td>Jobs</td><td>%s</td><td>Priority</td><td>%s</td></tr>", s.getNumberOfJobs(), c.getThreadPriority());
        pw.printf("<tr><td>Finished Jobs</td><td>%s</td><td colspan='2'>&nbsp</td></tr>", s.getNumberOfFinishedJobs());
        pw.printf("<tr><td>Failed Jobs</td><td>%s</td><td colspan='2'>&nbsp</td></tr>", s.getNumberOfFailedJobs());
        pw.printf("<tr><td>Cancelled Jobs</td><td>%s</td><td colspan='2'>&nbsp</td></tr>", s.getNumberOfCancelledJobs());
        pw.printf("<tr><td>Processed Jobs</td><td>%s</td><td colspan='2'>&nbsp</td></tr>", s.getNumberOfProcessedJobs());
        pw.printf("<tr><td>Average Processing Time</td><td>%s</td><td colspan='2'>&nbsp</td></tr>", formatTime(s.getAverageProcessingTime()));
        pw.printf("<tr><td>Average Waiting Time</td><td>%s</td><td colspan='2'>&nbsp</td></tr>", formatTime(s.getAverageWaitingTime()));
        pw.printf("<tr><td>Status Info</td><td colspan='3'>%s</td></tr>", ResponseUtil.escapeXml(q.getStateInfo()));
        pw.println("</tbody></table>");
        pw.println("<br/>");
    }
    if (isEmpty) {
        pw.println("<p>No active queues.</p>");
        pw.println("<br/>");
    }
    for (final TopicStatistics ts : this.jobManager.getTopicStatistics()) {
        pw.println("<table class='nicetable'><tbody>");
        pw.printf("<tr><th colspan='2'>Topic Statistics: %s</th></tr>", ResponseUtil.escapeXml(ts.getTopic()));
        pw.printf("<tr><td>Last Activated</td><td>%s</td></tr>", formatDate(ts.getLastActivatedJobTime()));
        pw.printf("<tr><td>Last Finished</td><td>%s</td></tr>", formatDate(ts.getLastFinishedJobTime()));
        pw.printf("<tr><td>Finished Jobs</td><td>%s</td></tr>", ts.getNumberOfFinishedJobs());
        pw.printf("<tr><td>Failed Jobs</td><td>%s</td></tr>", ts.getNumberOfFailedJobs());
        pw.printf("<tr><td>Cancelled Jobs</td><td>%s</td></tr>", ts.getNumberOfCancelledJobs());
        pw.printf("<tr><td>Processed Jobs</td><td>%s</td></tr>", ts.getNumberOfProcessedJobs());
        pw.printf("<tr><td>Average Processing Time</td><td>%s</td></tr>", formatTime(ts.getAverageProcessingTime()));
        pw.printf("<tr><td>Average Waiting Time</td><td>%s</td></tr>", formatTime(ts.getAverageWaitingTime()));
        pw.println("</tbody></table>");
        pw.println("<br/>");
    }
    pw.println("<p class='statline'>Apache Sling Job Handling - Job Queue Configurations</p>");
    this.printQueueConfiguration(req, pw, this.configuration.getQueueConfigurationManager().getMainQueueConfiguration());
    final InternalQueueConfiguration[] configs = this.configuration.getQueueConfigurationManager().getConfigurations();
    for (final InternalQueueConfiguration c : configs) {
        this.printQueueConfiguration(req, pw, c);
    }
}
Also used : TopologyCapabilities(org.apache.sling.event.impl.jobs.config.TopologyCapabilities) TopicStatistics(org.apache.sling.event.jobs.TopicStatistics) Statistics(org.apache.sling.event.jobs.Statistics) TopicStatistics(org.apache.sling.event.jobs.TopicStatistics) ScheduleInfo(org.apache.sling.event.jobs.ScheduleInfo) InternalQueueConfiguration(org.apache.sling.event.impl.jobs.config.InternalQueueConfiguration) ScheduledJobInfo(org.apache.sling.event.jobs.ScheduledJobInfo) QueueConfiguration(org.apache.sling.event.jobs.QueueConfiguration) InternalQueueConfiguration(org.apache.sling.event.impl.jobs.config.InternalQueueConfiguration) List(java.util.List) InstanceDescription(org.apache.sling.discovery.InstanceDescription) Map(java.util.Map) Queue(org.apache.sling.event.jobs.Queue) PrintWriter(java.io.PrintWriter)

Aggregations

InstanceDescription (org.apache.sling.discovery.InstanceDescription)59 ClusterView (org.apache.sling.discovery.ClusterView)16 DefaultInstanceDescription (org.apache.sling.discovery.commons.providers.DefaultInstanceDescription)11 Map (java.util.Map)10 PersistenceException (org.apache.sling.api.resource.PersistenceException)10 TopologyView (org.apache.sling.discovery.TopologyView)9 HashMap (java.util.HashMap)8 LoginException (org.apache.sling.api.resource.LoginException)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)8 Test (org.junit.Test)8 Announcement (org.apache.sling.discovery.base.connectors.announcement.Announcement)7 UndefinedClusterViewException (org.apache.sling.discovery.base.commons.UndefinedClusterViewException)6 DefaultClusterView (org.apache.sling.discovery.commons.providers.DefaultClusterView)6 HashSet (java.util.HashSet)5 Resource (org.apache.sling.api.resource.Resource)5 ValueMap (org.apache.sling.api.resource.ValueMap)5 LocalClusterView (org.apache.sling.discovery.commons.providers.spi.LocalClusterView)5 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)4 LinkedList (java.util.LinkedList)4