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.backup;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertNull;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024
025import java.io.IOException;
026import java.util.Arrays;
027import java.util.HashMap;
028import java.util.List;
029import java.util.Map;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.junit.jupiter.api.Tag;
033import org.junit.jupiter.api.Test;
034
035@Tag(SmallTests.TAG)
036public class TestBackupInfoSerialization {
037
038  private static BackupInfo newIncrementalBackupInfo() {
039    return new BackupInfo("backup_1234567890", BackupType.INCREMENTAL,
040      new TableName[] { TableName.valueOf("t1") }, "/hbase/backup");
041  }
042
043  @Test
044  public void testIncrBackupFileListSurvivesRoundTrip() throws IOException {
045    List<String> walFiles = Arrays.asList("/hbase/oldWALs/host1.example.com,16020,1234567890.100",
046      "/hbase/oldWALs/host2.example.com,16020,1234567890.200");
047
048    BackupInfo original = newIncrementalBackupInfo();
049    original.setIncrBackupFileList(walFiles);
050
051    BackupInfo roundTripped = BackupInfo.fromByteArray(original.toByteArray());
052
053    assertEquals(walFiles, roundTripped.getIncrBackupFileList());
054  }
055
056  @Test
057  public void testIncrBackupFileListOrderIsPreserved() throws IOException {
058    List<String> walFiles = Arrays.asList("/hbase/oldWALs/c.example.com,16020,1.300",
059      "/hbase/oldWALs/a.example.com,16020,1.100", "/hbase/oldWALs/b.example.com,16020,1.200");
060
061    BackupInfo original = newIncrementalBackupInfo();
062    original.setIncrBackupFileList(walFiles);
063
064    BackupInfo roundTripped = BackupInfo.fromByteArray(original.toByteArray());
065
066    assertEquals(walFiles, roundTripped.getIncrBackupFileList());
067  }
068
069  @Test
070  public void testUnsetIncrBackupFileListRemainsNull() throws IOException {
071    BackupInfo original = newIncrementalBackupInfo();
072
073    BackupInfo roundTripped = BackupInfo.fromByteArray(original.toByteArray());
074
075    assertNull(roundTripped.getIncrBackupFileList());
076  }
077
078  @Test
079  public void testEmptyIncrBackupFileListRemainsNull() throws IOException {
080    BackupInfo original = newIncrementalBackupInfo();
081    original.setIncrBackupFileList(Arrays.asList());
082
083    BackupInfo roundTripped = BackupInfo.fromByteArray(original.toByteArray());
084
085    assertNull(roundTripped.getIncrBackupFileList());
086  }
087
088  @Test
089  public void testEqualsDistinguishesIncrBackupFileList() throws IOException {
090    BackupInfo withFiles = newIncrementalBackupInfo();
091    withFiles.setIncrBackupFileList(Arrays.asList("/hbase/oldWALs/host1,16020,1.100"));
092
093    BackupInfo withoutFiles = newIncrementalBackupInfo();
094
095    assertTrue(!withFiles.equals(withoutFiles));
096  }
097
098  @Test
099  public void testTotalBytesCopiedSurvivesRoundTrip() throws IOException {
100    BackupInfo original = newIncrementalBackupInfo();
101    original.setTotalBytesCopied(9876543210L);
102
103    BackupInfo roundTripped = BackupInfo.fromByteArray(original.toByteArray());
104
105    assertEquals(9876543210L, roundTripped.getTotalBytesCopied());
106  }
107
108  @Test
109  public void testNoChecksumVerifySurvivesRoundTripWhenTrue() throws IOException {
110    BackupInfo original = newIncrementalBackupInfo();
111    original.setNoChecksumVerify(true);
112
113    BackupInfo roundTripped = BackupInfo.fromByteArray(original.toByteArray());
114
115    assertTrue(roundTripped.getNoChecksumVerify());
116  }
117
118  @Test
119  public void testNoChecksumVerifySurvivesRoundTripWhenFalse() throws IOException {
120    BackupInfo original = newIncrementalBackupInfo();
121    original.setNoChecksumVerify(false);
122
123    BackupInfo roundTripped = BackupInfo.fromByteArray(original.toByteArray());
124
125    assertFalse(roundTripped.getNoChecksumVerify());
126  }
127
128  @Test
129  public void testIncrTimestampMapSurvivesRoundTrip() throws IOException {
130    Map<String, Long> t1Timestamps = new HashMap<>();
131    t1Timestamps.put("host1.example.com:16020", 100L);
132    t1Timestamps.put("host2.example.com:16020", 200L);
133    Map<String, Long> t2Timestamps = new HashMap<>();
134    t2Timestamps.put("host1.example.com:16020", 300L);
135
136    Map<TableName, Map<String, Long>> incrTimestampMap = new HashMap<>();
137    incrTimestampMap.put(TableName.valueOf("t1"), t1Timestamps);
138    incrTimestampMap.put(TableName.valueOf("t2"), t2Timestamps);
139
140    BackupInfo original = newIncrementalBackupInfo();
141    original.setIncrTimestampMap(incrTimestampMap);
142
143    BackupInfo roundTripped = BackupInfo.fromByteArray(original.toByteArray());
144
145    assertEquals(incrTimestampMap, roundTripped.getIncrTimestampMap());
146  }
147
148  @Test
149  public void testUnsetIncrTimestampMapRemainsNull() throws IOException {
150    BackupInfo original = newIncrementalBackupInfo();
151
152    BackupInfo roundTripped = BackupInfo.fromByteArray(original.toByteArray());
153
154    assertNull(roundTripped.getIncrTimestampMap());
155  }
156
157  @Test
158  public void testIncrementalBackupRetainsHLogTargetDir() throws IOException {
159    BackupInfo original = newIncrementalBackupInfo();
160
161    BackupInfo roundTripped = BackupInfo.fromByteArray(original.toByteArray());
162
163    assertEquals(original.getHLogTargetDir(), roundTripped.getHLogTargetDir());
164  }
165
166}