Skip to content

Commit

Permalink
Merge pull request #335 from scoheb/missed-events
Browse files Browse the repository at this point in the history
JENKINS-39498 Fix invalid data
  • Loading branch information
rsandell committed Dec 6, 2017
2 parents 7a5f54b + 9242b6a commit fde68b3
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 20 deletions.
@@ -0,0 +1,79 @@
/*
* The MIT License
*
* Copyright (c) 2017 Red Hat
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.sonyericsson.hudson.plugins.gerrit.trigger.playback;

import com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer;
import com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl;
import hudson.Extension;
import hudson.model.AsyncPeriodicWork;
import hudson.model.TaskListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
* Utility class to periodically verify whether MissedEventsPlaybackManager
* can be enabled/disabled.
*/
@Extension
public class GerritMissedEventsPlaybackEnabledChecker extends AsyncPeriodicWork {

// default check period in seconds
private static final long DEFAULTCHECKPERIOD = 2;
private final Long recurrencePeriod;
private static final Logger logger =
LoggerFactory.getLogger(GerritMissedEventsPlaybackEnabledChecker.class);


/**
* Default constructor.
*/
public GerritMissedEventsPlaybackEnabledChecker() {
super("GerritMissedEventsPlaybackEnabledChecker");
recurrencePeriod =
Long.getLong("com.sonyericsson.hudson.plugins.gerrit.trigger.playback.checkEnabledPeriod",
TimeUnit.SECONDS.toMillis(DEFAULTCHECKPERIOD));
logger.debug("checkIfEventsLogPluginSupported check period is {0}ms",
recurrencePeriod);
}

@Override
protected void execute(TaskListener listener) throws IOException, InterruptedException {
List<GerritServer> servers = PluginImpl.getServers_();
for (GerritServer gs: servers) {
logger.debug("Performing plugin check for server: {0}", gs.getName());
gs.getMissedEventsPlaybackManager().performCheck();
}
}

@Override
public long getRecurrencePeriod() {
return recurrencePeriod;
}
}
Expand Up @@ -110,6 +110,33 @@ public GerritMissedEventsPlaybackManager(String name) {
checkIfEventsLogPluginSupported();
}

/**
* Method to perform check to see if Events Plugin is enabled.
* @throws IOException if occurs.
*/
public void performCheck() throws IOException {
if (playBackComplete) {
boolean previousIsSupported = isSupported;
checkIfEventsLogPluginSupported();
boolean currentIsSupported = isSupported;
if (previousIsSupported && !currentIsSupported) {
logger.warn("Missed Events Playback used to be supported. now it is not!");
// we could be missing events here that we should be persisting...
// so let's remove the data file so we are ready if it comes back
try {
XmlFile config = getConfigXml(serverName);
config.delete();
logger.warn("Deleting " + config.getFile().getAbsolutePath());
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
if (!previousIsSupported && currentIsSupported) {
logger.warn("Missed Events Playback used to be NOT supported. now it IS!");
}
}
}

/**
* The name of the {@link GerritServer} this is managing.
*
Expand All @@ -126,7 +153,7 @@ public void checkIfEventsLogPluginSupported() {
GerritServer server = PluginImpl.getServer_(serverName);
if (server != null && server.getConfig() != null) {
isSupported = GerritPluginChecker.isPluginEnabled(
server.getConfig(), EVENTS_LOG_PLUGIN_NAME);
server.getConfig(), EVENTS_LOG_PLUGIN_NAME, true);
}
}

Expand All @@ -138,6 +165,8 @@ protected void load() throws IOException {
XmlFile xml = getConfigXml(serverName);
if (xml.exists()) {
serverTimestamp = (EventTimeSlice)xml.unmarshal(serverTimestamp);
} else {
serverTimestamp = null;
}
}

Expand Down Expand Up @@ -517,4 +546,5 @@ public static XmlFile getConfigXml(String serverName) throws IOException {
public String getDisplayName() {
return StringUtil.getDefaultDisplayNameForSpecificServer(this, getServerName());
}

}
Expand Up @@ -77,37 +77,76 @@ private static String buildURL(IGerritHudsonTriggerConfig config) {
* Given a status code, decode its status.
* @param statusCode HTTP code
* @param pluginName plugin that was checked.
* @param quiet Should we log messages.
* @return true/false if installed or not.
*/
private static boolean decodeStatus(int statusCode, String pluginName) {
private static boolean decodeStatus(int statusCode, String pluginName, boolean quiet) {
String message = "";
switch (statusCode) {
case HttpURLConnection.HTTP_OK:
logger.info(Messages.PluginInstalled(pluginName));
message = Messages.PluginInstalled(pluginName);
if (quiet) {
logger.debug(message);
} else {
logger.info(message);
}
return true;
case HttpURLConnection.HTTP_NOT_FOUND:
logger.info(Messages.PluginNotInstalled(pluginName));
message = Messages.PluginNotInstalled(pluginName);
if (quiet) {
logger.debug(message);
} else {
logger.warn(message);
}
return false;
case HttpURLConnection.HTTP_UNAUTHORIZED:
logger.warn(Messages.PluginHttpConnectionUnauthorized(pluginName,
Messages.HttpConnectionUnauthorized()));
message = Messages.PluginHttpConnectionUnauthorized(pluginName,
Messages.HttpConnectionUnauthorized());
if (quiet) {
logger.debug(message);
} else {
logger.warn(message);
}
return false;
case HttpURLConnection.HTTP_FORBIDDEN:
logger.warn(Messages.PluginHttpConnectionForbidden(pluginName,
Messages.HttpConnectionUnauthorized()));
message = Messages.PluginHttpConnectionForbidden(pluginName,
Messages.HttpConnectionUnauthorized());
if (quiet) {
logger.debug(message);
} else {
logger.warn(message);
}
return false;
default:
logger.warn(Messages.PluginHttpConnectionGeneralError(pluginName,
Messages.HttpConnectionError(statusCode)));
message = Messages.PluginHttpConnectionGeneralError(pluginName,
Messages.HttpConnectionError(statusCode));
if (quiet) {
logger.debug(message);
} else {
logger.warn(message);
}
return false;
}
}

/**
* Query Gerrit to determine if plugin is enabled.
* @param config Gerrit Server Config
* @param pluginName The Gerrit Plugin name.
* @return true if enabled.
*/
public static boolean isPluginEnabled(IGerritHudsonTriggerConfig config, String pluginName) {
return isPluginEnabled(config, pluginName, false);
}

/**
* Query Gerrit to determine if plugin is enabled.
* @param config Gerrit Server Config
* @param pluginName The Gerrit Plugin name.
* @param quiet Whether we want to log a message.
* @return true if enabled.
*/
public static boolean isPluginEnabled(IGerritHudsonTriggerConfig config, String pluginName, boolean quiet) {

String restUrl = buildURL(config);
if (restUrl == null) {
Expand All @@ -121,7 +160,7 @@ public static boolean isPluginEnabled(IGerritHudsonTriggerConfig config, String
execute = HttpUtils.performHTTPGet(config, restUrl + "plugins/" + pluginName + "/");
int statusCode = execute.getStatusLine().getStatusCode();
logger.debug("status code: {}", statusCode);
return decodeStatus(statusCode, pluginName);
return decodeStatus(statusCode, pluginName, quiet);
} catch (IOException e) {
logger.warn(Messages.PluginHttpConnectionGeneralError(pluginName,
e.getMessage()), e);
Expand Down
Expand Up @@ -175,6 +175,7 @@ public static RefUpdated createRefUpdated(String serverName, String project, Str

return event;
}

/**
* Create a new patchset created event with the given data.
* @param serverName The server name
Expand All @@ -183,6 +184,21 @@ public static RefUpdated createRefUpdated(String serverName, String project, Str
* @return a pactchsetCreated event
*/
public static PatchsetCreated createPatchsetCreated(String serverName, String project, String ref) {
return createPatchsetCreated(serverName, project, ref, "1418133772");
}

/**
* Create a new patchset created event with the given data.
* @param serverName The server name
* @param project The project
* @param ref The ref
* @param eventCreateOn Timestamp for eventcreateon.
* @return a patchsetCreated event
*/
public static PatchsetCreated createPatchsetCreated(String serverName,
String project,
String ref,
String eventCreateOn) {
PatchsetCreated event = new PatchsetCreated();
Change change = new Change();
change.setBranch("branch");
Expand All @@ -202,7 +218,7 @@ public static PatchsetCreated createPatchsetCreated(String serverName, String pr
patch.setRef(ref);
event.setPatchset(patch);
event.setProvider(new Provider(serverName, "gerrit", "29418", "ssh", "http://gerrit/", "1"));
event.setEventCreatedOn("1418133772");
event.setEventCreatedOn(eventCreateOn);
return event;
}

Expand Down

0 comments on commit fde68b3

Please sign in to comment.