001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertNotSame;
023import static org.junit.Assert.assertSame;
024import static org.junit.Assert.assertTrue;
025
026import java.util.HashSet;
027import java.util.Set;
028import java.util.regex.Pattern;
029
030import org.apache.hadoop.hbase.testclassification.MiscTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.apache.hadoop.hbase.util.Addressing;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.ClassRule;
035import org.junit.Ignore;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039@Category({MiscTests.class, SmallTests.class})
040public class TestServerName {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044      HBaseClassTestRule.forClass(TestServerName.class);
045
046  @Test
047  public void testHash() {
048    ServerName sn1 = ServerName.parseServerName("asf903.gq1.ygridcore.net,52690,1517835491385");
049    ServerName sn2 = ServerName.parseServerName("asf903.gq1.ygridcore.net,42231,1517835491329");
050    Set<ServerName> sns = new HashSet<ServerName>();
051    sns.add(sn2);
052    sns.add(sn1);
053    sns.add(sn1);
054    assertEquals(2, sns.size());
055  }
056
057  @Test
058  public void testGetHostNameMinusDomain() {
059    assertEquals("2607:f0d0:1002:51::4",
060        ServerName.getHostNameMinusDomain("2607:f0d0:1002:51::4"));
061    assertEquals("2607:f0d0:1002:0051:0000:0000:0000:0004",
062        ServerName.getHostNameMinusDomain("2607:f0d0:1002:0051:0000:0000:0000:0004"));
063    assertEquals("1.1.1.1", ServerName.getHostNameMinusDomain("1.1.1.1"));
064    assertEquals("x", ServerName.getHostNameMinusDomain("x"));
065    assertEquals("x", ServerName.getHostNameMinusDomain("x.y.z"));
066    assertEquals("asf000", ServerName.getHostNameMinusDomain("asf000.sp2.ygridcore.net"));
067    ServerName sn = ServerName.valueOf("asf000.sp2.ygridcore.net", 1, 1);
068    assertEquals("asf000.sp2.ygridcore.net,1,1", sn.toString());
069  }
070
071  @Test
072  public void testShortString() {
073    ServerName sn = ServerName.valueOf("asf000.sp2.ygridcore.net", 1, 1);
074    assertEquals("asf000:1", sn.toShortString());
075    sn = ServerName.valueOf("2607:f0d0:1002:0051:0000:0000:0000:0004", 1, 1);
076    assertEquals("2607:f0d0:1002:0051:0000:0000:0000:0004:1", sn.toShortString());
077    sn = ServerName.valueOf("1.1.1.1", 1, 1);
078    assertEquals("1.1.1.1:1", sn.toShortString());
079  }
080
081  @Test
082  public void testRegexPatterns() {
083    assertTrue(Pattern.matches(Addressing.VALID_PORT_REGEX, "123"));
084    assertFalse(Pattern.matches(Addressing.VALID_PORT_REGEX, ""));
085    assertTrue(ServerName.SERVERNAME_PATTERN.matcher("www1.example.org,1234,567").matches());
086    ServerName.parseServerName("a.b.c,58102,1319771740322");
087    ServerName.parseServerName("192.168.1.199,58102,1319771740322");
088    ServerName.parseServerName("a.b.c:58102");
089    ServerName.parseServerName("192.168.1.199:58102");
090  }
091
092  @Test
093  public void testParseOfBytes() {
094    final String snStr = "www.EXAMPLE.org,1234,5678";
095    ServerName sn = ServerName.valueOf(snStr);
096    byte[] versionedBytes = sn.getVersionedBytes();
097    ServerName parsedSn = ServerName.parseVersionedServerName(versionedBytes);
098    assertEquals(sn.toString(), parsedSn.toString());
099    assertEquals(sn.getHostnameLowerCase(), parsedSn.getHostnameLowerCase());
100    assertEquals(sn.getPort(), parsedSn.getPort());
101    assertEquals(sn.getStartcode(), parsedSn.getStartcode());
102
103    final String hostnamePortStr = sn.getAddress().toString();
104    byte[] bytes = Bytes.toBytes(hostnamePortStr);
105    parsedSn = ServerName.parseVersionedServerName(bytes);
106    assertEquals(sn.getHostnameLowerCase(), parsedSn.getHostnameLowerCase());
107    assertEquals(sn.getPort(), parsedSn.getPort());
108    assertEquals(ServerName.NON_STARTCODE, parsedSn.getStartcode());
109  }
110
111  @Test
112  public void testServerName() {
113    ServerName sn = ServerName.valueOf("www.example.org", 1234, 5678);
114    ServerName sn2 = ServerName.valueOf("www.example.org", 1234, 5678);
115    ServerName sn3 = ServerName.valueOf("www.example.org", 1234, 56789);
116    assertTrue(sn.equals(sn2));
117    assertFalse(sn.equals(sn3));
118    assertEquals(sn.hashCode(), sn2.hashCode());
119    assertNotSame(sn.hashCode(), sn3.hashCode());
120    assertEquals(sn.toString(),
121        ServerName.valueOf("www.example.org", 1234, 5678).toString());
122    assertEquals(sn.toString(),
123        ServerName.valueOf("www.example.org:1234", 5678).toString());
124    assertEquals("www.example.org" + ServerName.SERVERNAME_SEPARATOR + "1234"
125        + ServerName.SERVERNAME_SEPARATOR + "5678", sn.toString());
126  }
127
128  @Test
129  public void testHostNameCaseSensitivity() {
130    ServerName lower = ServerName.valueOf("www.example.org", 1234, 5678);
131    ServerName upper = ServerName.valueOf("www.EXAMPLE.org", 1234, 5678);
132    assertEquals(0, lower.compareTo(upper));
133    assertEquals(0, upper.compareTo(lower));
134    assertEquals(lower.hashCode(), upper.hashCode());
135    assertTrue(lower.equals(upper));
136    assertTrue(upper.equals(lower));
137    assertTrue(ServerName.isSameAddress(lower, upper));
138  }
139
140  @Test
141  public void testInterning() {
142    ServerName sn1 = ServerName.valueOf("www.example.org", 1234, 5671);
143    assertSame(sn1, ServerName.valueOf("www.example.org", 1234, 5671));
144  }
145
146  @Ignore // Enable and let fun for hours to make sure weak references working fine.
147  @Test
148  public void testInterningDoesWeakReferences() {
149    for (int i = 0; i < Integer.MAX_VALUE; i++) {
150      ServerName.valueOf("www.example.org", 1234, i++);
151    }
152  }
153}
154