@Test
  public void testDefaultJoinGroupHelper() throws UnknownMasterException, DatabaseException {

    for (int i = 0; i < repEnvInfo.length; i++) {
      RepEnvInfo ri = repEnvInfo[i];
      if ((i + 1) == repEnvInfo.length) {
        /* Use a non-master helper for the last replicator. */
        ReplicationConfig config = RepTestUtils.createRepConfig((short) (i + 1));
        String hpPairs = "";
        // Skip the master, use all the other nodes
        for (int j = 1; j < i; j++) {
          hpPairs += "," + repEnvInfo[j].getRepConfig().getNodeHostPort();
        }
        hpPairs = hpPairs.substring(1);
        config.setHelperHosts(hpPairs);
        File envHome = ri.getEnvHome();
        ri =
            repEnvInfo[i] =
                new RepEnvInfo(
                    envHome, config, RepTestUtils.createEnvConfig(Durability.COMMIT_SYNC));
      }
      ri.openEnv();
      State state = ri.getEnv().getState();
      assertEquals((i == 0) ? State.MASTER : State.REPLICA, state);
    }
  }
Example #2
0
  @Override
  public void setValues(JSONObject formData) {
    gerritHostName = formData.optString("gerritHostName", DEFAULT_GERRIT_HOSTNAME);
    gerritSshPort = formData.optInt("gerritSshPort", DEFAULT_GERRIT_SSH_PORT);
    gerritProxy = formData.optString("gerritProxy", DEFAULT_GERRIT_PROXY);
    gerritUserName = formData.optString("gerritUserName", DEFAULT_GERRIT_USERNAME);
    gerritEMail = formData.optString("gerritEMail", "");
    notificationLevel =
        Notify.valueOf(
            formData.optString("notificationLevel", Config.DEFAULT_NOTIFICATION_LEVEL.toString()));
    String file = formData.optString("gerritAuthKeyFile", null);
    if (file != null) {
      gerritAuthKeyFile = new File(file);
    } else {
      gerritAuthKeyFile = DEFAULT_GERRIT_AUTH_KEY_FILE;
    }
    gerritAuthKeyFilePassword =
        Secret.fromString(
            formData.optString("gerritAuthKeyFilePassword", DEFAULT_GERRIT_AUTH_KEY_FILE_PASSWORD));

    if (formData.has("buildCurrentPatchesOnly")) {
      JSONObject currentPatchesOnly = formData.getJSONObject("buildCurrentPatchesOnly");
      buildCurrentPatchesOnly = BuildCancellationPolicy.createPolicyFromJSON(currentPatchesOnly);
    } else {
      buildCurrentPatchesOnly = new BuildCancellationPolicy();
    }

    numberOfWorkerThreads =
        formData.optInt("numberOfReceivingWorkerThreads", DEFAULT_NR_OF_RECEIVING_WORKER_THREADS);
    if (numberOfWorkerThreads <= 0) {
      numberOfWorkerThreads = DEFAULT_NR_OF_RECEIVING_WORKER_THREADS;
    }
    numberOfSendingWorkerThreads =
        formData.optInt("numberOfSendingWorkerThreads", DEFAULT_NR_OF_SENDING_WORKER_THREADS);
    if (numberOfSendingWorkerThreads <= 0) {
      numberOfSendingWorkerThreads = DEFAULT_NR_OF_SENDING_WORKER_THREADS;
    }

    if (formData.isEmpty()) {
      gerritBuildStartedVerifiedValue = DEFAULT_GERRIT_BUILD_STARTED_VERIFIED_VALUE;
      gerritBuildSuccessfulVerifiedValue = DEFAULT_GERRIT_BUILD_SUCCESSFUL_VERIFIED_VALUE;
      gerritBuildFailedVerifiedValue = DEFAULT_GERRIT_BUILD_FAILURE_VERIFIED_VALUE;
      gerritBuildUnstableVerifiedValue = DEFAULT_GERRIT_BUILD_UNSTABLE_VERIFIED_VALUE;
      gerritBuildNotBuiltVerifiedValue = DEFAULT_GERRIT_BUILD_NOT_BUILT_VERIFIED_VALUE;
      gerritBuildStartedCodeReviewValue = DEFAULT_GERRIT_BUILD_STARTED_CODE_REVIEW_VALUE;
      gerritBuildSuccessfulCodeReviewValue = DEFAULT_GERRIT_BUILD_SUCCESSFUL_CODE_REVIEW_VALUE;
      gerritBuildFailedCodeReviewValue = DEFAULT_GERRIT_BUILD_FAILURE_CODE_REVIEW_VALUE;
      gerritBuildUnstableCodeReviewValue = DEFAULT_GERRIT_BUILD_UNSTABLE_CODE_REVIEW_VALUE;
      gerritBuildNotBuiltCodeReviewValue = DEFAULT_GERRIT_BUILD_NOT_BUILT_CODE_REVIEW_VALUE;
    } else {
      gerritBuildStartedVerifiedValue =
          getValueFromFormData(formData, "gerritBuildStartedVerifiedValue");
      gerritBuildSuccessfulVerifiedValue =
          getValueFromFormData(formData, "gerritBuildSuccessfulVerifiedValue");
      gerritBuildFailedVerifiedValue =
          getValueFromFormData(formData, "gerritBuildFailedVerifiedValue");
      gerritBuildUnstableVerifiedValue =
          getValueFromFormData(formData, "gerritBuildUnstableVerifiedValue");
      gerritBuildNotBuiltVerifiedValue =
          getValueFromFormData(formData, "gerritBuildNotBuiltVerifiedValue");
      gerritBuildStartedCodeReviewValue =
          getValueFromFormData(formData, "gerritBuildStartedCodeReviewValue");
      gerritBuildSuccessfulCodeReviewValue =
          getValueFromFormData(formData, "gerritBuildSuccessfulCodeReviewValue");
      gerritBuildFailedCodeReviewValue =
          getValueFromFormData(formData, "gerritBuildFailedCodeReviewValue");
      gerritBuildUnstableCodeReviewValue =
          getValueFromFormData(formData, "gerritBuildUnstableCodeReviewValue");
      gerritBuildNotBuiltCodeReviewValue =
          getValueFromFormData(formData, "gerritBuildNotBuiltCodeReviewValue");
    }

    gerritVerifiedCmdBuildStarted =
        formData.optString(
            "gerritVerifiedCmdBuildStarted",
            "gerrit review <CHANGE>,<PATCHSET> --message 'Build Started <BUILDURL> <STARTED_STATS>' "
                + "--verified <VERIFIED> --code-review <CODE_REVIEW>");
    gerritVerifiedCmdBuildFailed =
        formData.optString(
            "gerritVerifiedCmdBuildFailed",
            "gerrit review <CHANGE>,<PATCHSET> --message 'Build Failed <BUILDS_STATS>' "
                + "--verified <VERIFIED> --code-review <CODE_REVIEW>");
    gerritVerifiedCmdBuildSuccessful =
        formData.optString(
            "gerritVerifiedCmdBuildSuccessful",
            "gerrit review <CHANGE>,<PATCHSET> --message 'Build Successful <BUILDS_STATS>' "
                + "--verified <VERIFIED> --code-review <CODE_REVIEW>");
    gerritVerifiedCmdBuildUnstable =
        formData.optString(
            "gerritVerifiedCmdBuildUnstable",
            "gerrit review <CHANGE>,<PATCHSET> --message 'Build Unstable <BUILDS_STATS>' "
                + "--verified <VERIFIED> --code-review <CODE_REVIEW>");
    gerritVerifiedCmdBuildNotBuilt =
        formData.optString(
            "gerritVerifiedCmdBuildNotBuilt",
            "gerrit review <CHANGE>,<PATCHSET> --message 'No Builds Executed <BUILDS_STATS>' "
                + "--verified <VERIFIED> --code-review <CODE_REVIEW>");
    gerritFrontEndUrl = formData.optString("gerritFrontEndUrl", DEFAULT_GERRIT_HOSTNAME);
    enableManualTrigger = formData.optBoolean("enableManualTrigger", DEFAULT_ENABLE_MANUAL_TRIGGER);
    enablePluginMessages =
        formData.optBoolean("enablePluginMessages", DEFAULT_ENABLE_PLUGIN_MESSAGES);
    buildScheduleDelay = formData.optInt("buildScheduleDelay", DEFAULT_BUILD_SCHEDULE_DELAY);
    if (buildScheduleDelay < 0) {
      buildScheduleDelay = 0;
    }
    dynamicConfigRefreshInterval =
        formData.optInt("dynamicConfigRefreshInterval", DEFAULT_DYNAMIC_CONFIG_REFRESH_INTERVAL);

    projectListFetchDelay =
        formData.optInt("projectListFetchDelay", DEFAULT_PROJECT_LIST_FETCH_DELAY);

    projectListRefreshInterval =
        formData.optInt("projectListRefreshInterval", DEFAULT_PROJECT_LIST_REFRESH_INTERVAL);
    enableProjectAutoCompletion =
        formData.optBoolean("enableProjectAutoCompletion", DEFAULT_ENABLE_PROJECT_AUTO_COMPLETION);

    categories = new LinkedList<VerdictCategory>();
    if (formData.has("verdictCategories")) {
      Object cat = formData.get("verdictCategories");
      if (cat instanceof JSONArray) {
        for (Object jsonObject : (JSONArray) cat) {
          categories.add(VerdictCategory.createVerdictCategoryFromJSON((JSONObject) jsonObject));
        }
      } else if (cat instanceof JSONObject) {
        categories.add(VerdictCategory.createVerdictCategoryFromJSON((JSONObject) cat));
      }
    }
    watchdogTimeoutMinutes =
        formData.optInt("watchdogTimeoutMinutes", DEFAULT_GERRIT_WATCHDOG_TIMEOUT_MINUTES);
    watchTimeExceptionData = addWatchTimeExceptionData(formData);

    if (formData.has("useRestApi")) {
      useRestApi = true;
      JSONObject restApi = formData.getJSONObject("useRestApi");
      gerritHttpUserName = restApi.optString("gerritHttpUserName", "");
      gerritHttpPassword = Secret.fromString(restApi.optString("gerritHttpPassword", ""));
      restCodeReview = restApi.optBoolean("restCodeReview", true);
      restVerified = restApi.optBoolean("restVerified", true);
    } else {
      useRestApi = false;
    }

    replicationConfig = ReplicationConfig.createReplicationConfigFromJSON(formData);
  }
  @SuppressWarnings("null")
  @Test
  public void testBasic() throws InterruptedException {

    final int dataNodeSize = groupSize - 1;
    final int electableNodeSize = groupSize - 2;
    final int persistentNodeSize = groupSize - 1;

    final ReplicationConfig sConfig = repEnvInfo[groupSize - 2].getRepConfig();
    sConfig.setNodeType(NodeType.SECONDARY);

    createGroup(dataNodeSize);

    ReplicationConfig rConfig = repEnvInfo[groupSize - 1].getRepConfig();
    /* RepNetConfig needs to come from an open environment */
    ReplicationConfig r0Config = repEnvInfo[0].getEnv().getRepConfig();
    rConfig.setNodeType(NodeType.MONITOR);
    MonitorConfig monConfig = new MonitorConfig();
    monConfig.setNodeName(rConfig.getNodeName());
    monConfig.setGroupName(rConfig.getGroupName());
    monConfig.setNodeHostPort(rConfig.getNodeHostPort());
    monConfig.setHelperHosts(rConfig.getHelperHosts());
    monConfig.setRepNetConfig(r0Config.getRepNetConfig());

    new Monitor(monConfig).register();

    for (int i = 0; i < dataNodeSize; i++) {
      final ReplicatedEnvironment env = repEnvInfo[i].getEnv();
      final boolean isMaster = (env.getState() == MASTER);
      final int targetGroupSize = isMaster ? groupSize : persistentNodeSize;
      ReplicationGroup group = null;
      for (int j = 0; j < 100; j++) {
        group = env.getGroup();
        if (group.getNodes().size() == targetGroupSize) {
          break;
        }
        /* Wait for the replica to catch up. */
        Thread.sleep(1000);
      }
      assertEquals("Nodes", targetGroupSize, group.getNodes().size());
      assertEquals(RepTestUtils.TEST_REP_GROUP_NAME, group.getName());
      logger.info(group.toString());

      for (RepEnvInfo rinfo : repEnvInfo) {
        final ReplicationConfig repConfig = rinfo.getRepConfig();
        ReplicationNode member = group.getMember(repConfig.getNodeName());
        if (!isMaster && repConfig.getNodeType().isSecondary()) {
          assertNull("Member", member);
        } else {
          assertNotNull("Member", member);
          assertEquals(repConfig.getNodeName(), member.getName());
          assertEquals(repConfig.getNodeType(), member.getType());
          assertEquals(repConfig.getNodeSocketAddress(), member.getSocketAddress());
        }
      }

      final Set<ReplicationNode> electableNodes = group.getElectableNodes();
      for (final ReplicationNode n : electableNodes) {
        assertEquals(NodeType.ELECTABLE, n.getType());
      }
      assertEquals("Electable nodes", electableNodeSize, electableNodes.size());

      final Set<ReplicationNode> monitorNodes = group.getMonitorNodes();
      for (final ReplicationNode n : monitorNodes) {
        assertEquals(NodeType.MONITOR, n.getType());
      }
      assertEquals("Monitor nodes", 1, monitorNodes.size());

      final Set<ReplicationNode> secondaryNodes = group.getSecondaryNodes();
      for (final ReplicationNode n : secondaryNodes) {
        assertEquals(NodeType.SECONDARY, n.getType());
      }
      assertEquals("Secondary nodes", isMaster ? 1 : 0, secondaryNodes.size());

      final Set<ReplicationNode> dataNodes = group.getDataNodes();
      for (final ReplicationNode n : dataNodes) {
        if (isMaster) {
          assertThat(n.getType(), anyOf(is(NodeType.ELECTABLE), is(NodeType.SECONDARY)));
        } else {
          assertEquals(NodeType.ELECTABLE, n.getType());
        }
      }
      assertEquals("Data nodes", isMaster ? dataNodeSize : electableNodeSize, dataNodes.size());
    }
  }