Search in sources :

Example 6 with Pattern

use of java.util.regex.Pattern in project flink by apache.

the class ZooKeeperLeaderElectionTest method testZooKeeperReelectionWithReplacement.

/**
	 * Tests the repeated reelection of {@link LeaderContender} once the current leader dies.
	 * Furthermore, it tests that new LeaderElectionServices can be started later on and that they
	 * successfully register at ZooKeeper and take part in the leader election.
	 */
@Test
public void testZooKeeperReelectionWithReplacement() throws Exception {
    Configuration configuration = new Configuration();
    configuration.setString(HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM, testingServer.getConnectString());
    configuration.setString(HighAvailabilityOptions.HA_MODE, "zookeeper");
    int num = 3;
    int numTries = 30;
    ZooKeeperLeaderElectionService[] leaderElectionService = new ZooKeeperLeaderElectionService[num];
    TestingContender[] contenders = new TestingContender[num];
    ZooKeeperLeaderRetrievalService leaderRetrievalService = null;
    TestingListener listener = new TestingListener();
    try {
        leaderRetrievalService = ZooKeeperUtils.createLeaderRetrievalService(configuration);
        leaderRetrievalService.start(listener);
        for (int i = 0; i < num; i++) {
            leaderElectionService[i] = ZooKeeperUtils.createLeaderElectionService(configuration);
            contenders[i] = new TestingContender(TEST_URL + "_" + i + "_0", leaderElectionService[i]);
            leaderElectionService[i].start(contenders[i]);
        }
        String pattern = TEST_URL + "_" + "(\\d+)" + "_" + "(\\d+)";
        Pattern regex = Pattern.compile(pattern);
        for (int i = 0; i < numTries; i++) {
            listener.waitForNewLeader(timeout.toMillis());
            String address = listener.getAddress();
            Matcher m = regex.matcher(address);
            if (m.find()) {
                int index = Integer.parseInt(m.group(1));
                int lastTry = Integer.parseInt(m.group(2));
                assertEquals(listener.getLeaderSessionID(), contenders[index].getLeaderSessionID());
                // stop leader election service = revoke leadership
                leaderElectionService[index].stop();
                // create new leader election service which takes part in the leader election
                leaderElectionService[index] = ZooKeeperUtils.createLeaderElectionService(configuration);
                contenders[index] = new TestingContender(TEST_URL + "_" + index + "_" + (lastTry + 1), leaderElectionService[index]);
                leaderElectionService[index].start(contenders[index]);
            } else {
                throw new Exception("Did not find the leader's index.");
            }
        }
    } finally {
        if (leaderRetrievalService != null) {
            leaderRetrievalService.stop();
        }
        for (ZooKeeperLeaderElectionService electionService : leaderElectionService) {
            if (electionService != null) {
                electionService.stop();
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Configuration(org.apache.flink.configuration.Configuration) Matcher(java.util.regex.Matcher) ZooKeeperLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalService) TimeoutException(java.util.concurrent.TimeoutException) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 7 with Pattern

use of java.util.regex.Pattern in project flink by apache.

the class ZooKeeperLeaderElectionTest method testZooKeeperReelection.

/**
	 * Tests repeatedly the reelection of still available LeaderContender. After a contender has
	 * been elected as the leader, it is removed. This forces the ZooKeeperLeaderElectionService
	 * to elect a new leader.
	 */
@Test
public void testZooKeeperReelection() throws Exception {
    Configuration configuration = new Configuration();
    configuration.setString(HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM, testingServer.getConnectString());
    configuration.setString(HighAvailabilityOptions.HA_MODE, "zookeeper");
    Deadline deadline = new FiniteDuration(5, TimeUnit.MINUTES).fromNow();
    int num = 20;
    ZooKeeperLeaderElectionService[] leaderElectionService = new ZooKeeperLeaderElectionService[num];
    TestingContender[] contenders = new TestingContender[num];
    ZooKeeperLeaderRetrievalService leaderRetrievalService = null;
    TestingListener listener = new TestingListener();
    try {
        leaderRetrievalService = ZooKeeperUtils.createLeaderRetrievalService(configuration);
        LOG.debug("Start leader retrieval service for the TestingListener.");
        leaderRetrievalService.start(listener);
        for (int i = 0; i < num; i++) {
            leaderElectionService[i] = ZooKeeperUtils.createLeaderElectionService(configuration);
            contenders[i] = new TestingContender(TEST_URL + "_" + i, leaderElectionService[i]);
            LOG.debug("Start leader election service for contender #{}.", i);
            leaderElectionService[i].start(contenders[i]);
        }
        String pattern = TEST_URL + "_" + "(\\d+)";
        Pattern regex = Pattern.compile(pattern);
        int numberSeenLeaders = 0;
        while (deadline.hasTimeLeft() && numberSeenLeaders < num) {
            LOG.debug("Wait for new leader #{}.", numberSeenLeaders);
            String address = listener.waitForNewLeader(deadline.timeLeft().toMillis());
            Matcher m = regex.matcher(address);
            if (m.find()) {
                int index = Integer.parseInt(m.group(1));
                TestingContender contender = contenders[index];
                // check that the retrieval service has retrieved the correct leader
                if (address.equals(contender.getAddress()) && listener.getLeaderSessionID().equals(contender.getLeaderSessionID())) {
                    // kill the election service of the leader
                    LOG.debug("Stop leader election service of contender #{}.", numberSeenLeaders);
                    leaderElectionService[index].stop();
                    leaderElectionService[index] = null;
                    numberSeenLeaders++;
                }
            } else {
                fail("Did not find the leader's index.");
            }
        }
        assertFalse(deadline.isOverdue());
        assertEquals(num, numberSeenLeaders);
    } finally {
        if (leaderRetrievalService != null) {
            leaderRetrievalService.stop();
        }
        for (ZooKeeperLeaderElectionService electionService : leaderElectionService) {
            if (electionService != null) {
                electionService.stop();
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Configuration(org.apache.flink.configuration.Configuration) Matcher(java.util.regex.Matcher) Deadline(scala.concurrent.duration.Deadline) FiniteDuration(scala.concurrent.duration.FiniteDuration) ZooKeeperLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalService) Test(org.junit.Test)

Example 8 with Pattern

use of java.util.regex.Pattern in project hadoop by apache.

the class TestKerberosUtil method testGetPrincipalNamesFromKeytabWithPattern.

@Test
public void testGetPrincipalNamesFromKeytabWithPattern() throws IOException {
    createKeyTab(testKeytab, testPrincipals);
    // read the keytab file
    // look for principals with HTTP as the first part
    Pattern httpPattern = Pattern.compile("HTTP/.*");
    String[] httpPrincipals = KerberosUtil.getPrincipalNames(testKeytab, httpPattern);
    Assert.assertNotNull("principals cannot be null", httpPrincipals);
    int expectedSize = 0;
    List<String> httpPrincipalList = Arrays.asList(httpPrincipals);
    for (String principal : testPrincipals) {
        if (httpPattern.matcher(principal).matches()) {
            Assert.assertTrue("missing principal " + principal, httpPrincipalList.contains(principal));
            expectedSize++;
        }
    }
    Assert.assertEquals(expectedSize, httpPrincipals.length);
}
Also used : Pattern(java.util.regex.Pattern) Test(org.junit.Test)

Example 9 with Pattern

use of java.util.regex.Pattern in project hadoop by apache.

the class TestConfiguration method testPattern.

public void testPattern() throws IOException {
    out = new BufferedWriter(new FileWriter(CONFIG));
    startConfig();
    appendProperty("test.pattern1", "");
    appendProperty("test.pattern2", "(");
    appendProperty("test.pattern3", "a+b");
    endConfig();
    Path fileResource = new Path(CONFIG);
    conf.addResource(fileResource);
    Pattern defaultPattern = Pattern.compile("x+");
    // Return default if missing
    assertEquals(defaultPattern.pattern(), conf.getPattern("xxxxx", defaultPattern).pattern());
    // Return null if empty and default is null
    assertNull(conf.getPattern("test.pattern1", null));
    // Return default for empty
    assertEquals(defaultPattern.pattern(), conf.getPattern("test.pattern1", defaultPattern).pattern());
    // Return default for malformed
    assertEquals(defaultPattern.pattern(), conf.getPattern("test.pattern2", defaultPattern).pattern());
    // Works for correct patterns
    assertEquals("a+b", conf.getPattern("test.pattern3", defaultPattern).pattern());
}
Also used : Path(org.apache.hadoop.fs.Path) Pattern(java.util.regex.Pattern) FileWriter(java.io.FileWriter) BufferedWriter(java.io.BufferedWriter)

Example 10 with Pattern

use of java.util.regex.Pattern in project hadoop by apache.

the class TestConfigurationFieldsBase method extractMemberVariablesFromConfigurationFields.

/**
   * Utility function to extract &quot;public static final&quot; member
   * variables from a Configuration type class.
   *
   * @param fields The class member variables
   * @return HashMap containing <StringValue,MemberVariableName> entries
   */
private HashMap<String, String> extractMemberVariablesFromConfigurationFields(Field[] fields) {
    // Sanity Check
    if (fields == null)
        return null;
    HashMap<String, String> retVal = new HashMap<String, String>();
    // Setup regexp for valid properties
    String propRegex = "^[A-Za-z][A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)+$";
    Pattern p = Pattern.compile(propRegex);
    // Iterate through class member variables
    int totalFields = 0;
    String value;
    for (Field f : fields) {
        if (configDebug) {
            System.out.println("Field: " + f);
        }
        // Filter out anything that isn't "public static final"
        if (!Modifier.isStatic(f.getModifiers()) || !Modifier.isPublic(f.getModifiers()) || !Modifier.isFinal(f.getModifiers())) {
            continue;
        }
        // default values
        if (!f.getType().getName().equals("java.lang.String")) {
            continue;
        }
        // filter out default-value fields
        if (isFieldADefaultValue(f)) {
            continue;
        }
        // Convert found member into String
        try {
            value = (String) f.get(null);
        } catch (IllegalAccessException iaException) {
            continue;
        }
        if (configDebug) {
            System.out.println("  Value: " + value);
        }
        //               or file properties (ending in .xml)
        if (value.endsWith(".xml") || value.endsWith(".") || value.endsWith("-"))
            continue;
        // Ignore known configuration props
        if (configurationPropsToSkipCompare != null) {
            if (configurationPropsToSkipCompare.contains(value)) {
                continue;
            }
        }
        // Ignore known configuration prefixes
        boolean skipPrefix = false;
        if (configurationPrefixToSkipCompare != null) {
            for (String cfgPrefix : configurationPrefixToSkipCompare) {
                if (value.startsWith(cfgPrefix)) {
                    skipPrefix = true;
                    break;
                }
            }
        }
        if (skipPrefix) {
            continue;
        }
        // Positive Filter: Look only for property values.  Expect it to look
        //                  something like: blah.blah2(.blah3.blah4...)
        Matcher m = p.matcher(value);
        if (!m.find()) {
            if (configDebug) {
                System.out.println("  Passes Regex: false");
            }
            continue;
        }
        if (configDebug) {
            System.out.println("  Passes Regex: true");
        }
        // Save member variable/value as hash
        if (!retVal.containsKey(value)) {
            retVal.put(value, f.getName());
        } else {
            if (configDebug) {
                System.out.println("ERROR: Already found key for property " + value);
            }
        }
    }
    return retVal;
}
Also used : Pattern(java.util.regex.Pattern) Field(java.lang.reflect.Field) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher)

Aggregations

Pattern (java.util.regex.Pattern)2745 Matcher (java.util.regex.Matcher)1806 ArrayList (java.util.ArrayList)334 Test (org.junit.Test)209 IOException (java.io.IOException)206 File (java.io.File)172 HashMap (java.util.HashMap)144 Field (java.lang.reflect.Field)118 BufferedReader (java.io.BufferedReader)108 Map (java.util.Map)97 PatternSyntaxException (java.util.regex.PatternSyntaxException)97 List (java.util.List)81 HashSet (java.util.HashSet)69 InputStreamReader (java.io.InputStreamReader)59 FileInputStream (java.io.FileInputStream)40 InputStream (java.io.InputStream)39 FileReader (java.io.FileReader)36 SmallTest (android.test.suitebuilder.annotation.SmallTest)31 URL (java.net.URL)29 LinkedHashMap (java.util.LinkedHashMap)28