×
Community Blog Alibaba Cloud Tablestore: A Case Study on Backing up a Massive Volume of Structured Data

Alibaba Cloud Tablestore: A Case Study on Backing up a Massive Volume of Structured Data

his article describes the backup and restoration principles involved with Tunnel Service and provides a step-by-step guide to making a data backup and restoration solution.

Abstract: This article describes the backup and restoration principles involved with Tunnel Service and provides a step-by-step guide to making a data backup and restoration solution.

By Wang Zhuoran, nicknamed Zhuoran at Alibaba.

In the field of information technology and data management, the term backup implies the replication of data in a file system or database system to restore the requisite data and routine operations of the system in the event of a disaster or an error. What this all means is that, in actual backup scenarios, it is recommended that you back up important data in three or more replicas, and store them in different locations for geo-redundancy purpose.

Backup fundamentally involves two different items. The first one is to restore lost data due to mis-deletion or corruption, and the second one is to restore older data based on a custom data retention policy. Generally, the configuration of replication scope is done in the backup application.

Another important point to know is that a backup system may have high requirements for data storage as a system will contain at least one replica consisting of all the data worth storing. It may be complex to organize this storage space and manage backups. Currently, there are many different types of data storage devices available for backup on the market. These devices are configurable in several different ways to ensure geo-redundancy, data security, as well as portability.

Before sending data to a storage location, generally the processes of effective selection, extraction, and the related operation are required. You can optimize the backup process using a variety of technologies, including open file processing, real-time data source optimization, and data compression, encryption, and deduplication. Each backup solution must necessarily provide for drilling scenarios so to be able to verify the reliability of the data being backed up and, more importantly, to reveal the limits and human factors involved in the backup solution.

Now that we are on the same page when it comes to data backup and restoration, let's discuss the backup and restoration principles involved with Tunnel Service, which is part of Alibaba Cloud Tablestore. And, in this article, we're also going to provide you with a step-by-step guide on how to make a data backup and restoration solution using Tablestore and Tunnel Service.

Requirements

Storage systems by their very nature must necessarily give top priority to data security and reliability. And, as such, they must ensure the following guarantees:

  • Data Reliability of Storage Systems: Alibaba Cloud Tablestore is a serverless NoSQL multi-model database independently developed by Alibaba Cloud for storing massive structured data. It provides 99.9999999% data reliability, which is a very high standard in the industry.
  • Data Restoration After a Misoperation: A misoperation at some point is inevitable. Data backup restores data as soon as possible when a misoperation occurs. Two backup solutions are available. The first one is to deploy local disaster recovery or geo-disaster recovery. This can be costly and is primarily used for backup of basic social or financial information. The second one is to back up data to another inexpensive system for restoring the data in case of misoperation. For the latter option, users generally go with a file storage system like Alibaba Cloud Object Storage Service (OSS).

Tablestore Backup and Restoration Solution

The following figure shows the logical structure of backup and restoration through Alibaba Cloud Tablestore. Tunnel Service. This service integrates the full mode and incremental mode, allowing you more easily build a complete data backup and restoration solution thanks to real-time incremental backup and second-level restoration capabilities. With this service, the entire backup and restoration system is fully automated after the backup and restoration plan configuration.

1

Case Study: Tablestore Backup and Restoration Solution

Currently, Alibaba Cloud Tablestore does not provide official backup and restoration functions. So, let's take a look at how to design a custom backup and restoration solution for Tablestore based on the Tunnel Service of Tablestore. The actual procedure is divided into two parts: backup and recovery.

1. Backup Preparation: Determine the data source to back up and the destination location. In this case study, the data source to back up is tables in Alibaba Cloud Tablestore, and the destination location is buckets in Alibaba Cloud OSS. For this case study, we will be doing the following:

  • Determine a backup plan and policy.
  • Use the Tunnel Service SDK to write code.
  • Execute the backup policy and monitor the backup process.

2. Recovery Last, we need to do the recovery step. For this step, all we need to do is restore the files.

Determining a Backup Plan and Policy

A backup policy specifies the backup content, backup time, and backup method. Common backup policies are as follows:

  • Full Backup: This copies all the files, folders, or data on a hard disk or database at once.
  • Incremental Backup: This backs up subsequently updated data after the last full or incremental backup.
  • Differential Backup: This backs up changed files after a full backup.
  • Selective Backup: This backs up a part of the system data.
  • Cold Backup: This acks up data when the system is in a stopped or maintenance state. The backup data and the system data from this period must necessarily be the same.
  • Hot Backup: This backs up data when the system runs properly. The system data updates at any time so that the backup data may lag behind the actual system data.

Additionally note that this case study uses the following backup plan and policy: you can customize a backup plan and policy as needed and use the Tunnel Service SDK to write related code.

  • Backup Content: Data tables in Tablestore
  • Backup Time: This includes the following:
  1. A full backup is performed periodically. The interval is adjustable and the default value is one week.
  2. Incremental backup is also performed periodically based on the configuration. Tunnel Service of Tablestore ensures a strict data order. Each incremental file is appended to the stream and is consumed at any time.
  • Backup Methods: There's full backup, incremental backup, and hot backup.

Using the Tunnel Service SDK to Write Code

This step is described based on code snippets. Detailed code will be open-source later (links will be updated in this article). It's recommended that you read the user manual of the Tunnel Service SDK in advance.

1. Create a tunnel of the full-incremental type using the SDK or within the console.

private static void createTunnel(TunnelClient client, String tunnelName) {
    CreateTunnelRequest request = new CreateTunnelRequest(TableName, tunnelName, TunnelType.BaseAndStream);
    CreateTunnelResponse resp = client.createTunnel(request);
    System.out.println("RequestId: " + resp.getRequestId());
    System.out.println("TunnelId: " + resp.getTunnelId());
}

2. Familiarize yourself with the automatic data processing framework of Tunnel Service

Enter the process function and shutdown function for automatic data processing based on the instructions in the Quick Start Guide of Tunnel Service. The input parameters of the process function contain a list. StreamRecord encapsulates the data of each row in Tablestore, including the record type, primary key column, property column, and timestamp when you write records.

3. Design the persistence format for each row of data in Tablestore

This case study uses the CSV file format, which makes data fully accessible. You may also use PB or other formats. Each data row corresponds to a row in a CSV file. The persistent format is shown in the following figure. A CSV file has four columns. The TimeStamp column contains the timestamp when data is written to Tablestore (the value is 0 for a full back up or a specific timestamp for incremental backup). RecordType indicates the action type (PUT, UPDATE, or DELETE) of the data row. PrimaryKey is the JSON string of the primary key column. RecordColumns is the JSON string of the property column.

2

The following code snippet is the core code of conversion and processed using uniVocity-parsers (CSV) and Gson. Note the following points:

  1. Progress loss may occur when Gson deserializes the Long type into the Number type. However, it is possible to resolve this problem by using several different methods. One effective method is to serialize the Long type into the String type.
  2. Base64 encodes and decodes the binary data.
  3. Write the data directly to OSS, which reduces resource consumption during local persistence.
this.gson = new GsonBuilder().registerTypeHierarchyAdapter(byte[].class, new ByteArrayToBase64TypeAdapter())
           .setLongSerializationPolicy(LongSerializationPolicy.STRING).create();
// ByteArrayOutputStream到ByteArrayInputStream会有一次array.copy, 可考虑用管道或者NIO channel.
public void streamRecordsToOSS(List<StreamRecord> records, String bucketName, String filename, boolean isNewFile) {
   if (records.size() == 0) {
       LOG.info("No stream records, skip it!");
       return;
   }
   try {
       CsvWriterSettings settings = new CsvWriterSettings();
       ByteArrayOutputStream out = new ByteArrayOutputStream();
       CsvWriter writer = new CsvWriter(out, settings);
       if (isNewFile) {
           LOG.info("Write csv header, filename {}", filename);
           List<String> headers = Arrays.asList(RECORD_TIMESTAMP, RECORD_TYPE, PRIMARY_KEY, RECORD_COLUMNS);
           writer.writeHeaders(headers);
           System.out.println(writer.getRecordCount());
       }
       List<String[]> totalRows = new ArrayList<String[]>();
       LOG.info("Write stream records, num: {}", records.size());
       for (StreamRecord record : records) {
           String timestamp = String.valueOf(record.getSequenceInfo().getTimestamp());
           String recordType = record.getRecordType().name();
           String primaryKey = gson.toJson(
               TunnelPrimaryKeyColumn.genColumns(record.getPrimaryKey().getPrimaryKeyColumns()));
           String columns = gson.toJson(TunnelRecordColumn.genColumns(record.getColumns()));
           totalRows.add(new String[] {timestamp, recordType, primaryKey, columns});
       }
       writer.writeStringRowsAndClose(totalRows);

       // write to oss file
       ossClient.putObject(bucketName, filename, new ByteArrayInputStream(out.toByteArray()));
   } catch (Exception e) {
       e.printStackTrace();
   }
}

Executing the Backup Policy and Monitor the Backup Process

1. Run the automatic data framework of the Tunnel Service to mount the backup policy code designed in the previous step.

public class TunnelBackup {
    private final ConfigHelper config;
    private final SyncClient syncClient;
    private final CsvHelper csvHelper;
    private final OSSClient ossClient;

    public TunnelBackup(ConfigHelper config) {
        this.config = config;
        syncClient = new SyncClient(config.getEndpoint(), config.getAccessId(), config.getAccessKey(),
            config.getInstanceName());
        ossClient = new OSSClient(config.getOssEndpoint(), config.getAccessId(), config.getAccessKey());
        csvHelper = new CsvHelper(syncClient, ossClient);
    }

    public void working() {
        TunnelClient client = new TunnelClient(config.getEndpoint(), config.getAccessId(), config.getAccessKey(),
            config.getInstanceName());
        OtsReaderConfig readerConfig = new OtsReaderConfig();
        TunnelWorkerConfig workerConfig = new TunnelWorkerConfig(
            new OtsReaderProcessor(csvHelper, config.getOssBucket(), readerConfig));
        TunnelWorker worker = new TunnelWorker(config.getTunnelId(), client, workerConfig);
        try {
            worker.connectAndWorking();
        } catch (Exception e) {
            e.printStackTrace();
            worker.shutdown();
            client.shutdown();
        }
    }

    public static void main(String[] args) {
        TunnelBackup tunnelBackup = new TunnelBackup(new ConfigHelper());
        tunnelBackup.working();
    }
}

2. Monitor the backup process

Monitor the backup process in the Tablestore console or use the DescribeTunnel operation to obtain client information (customizable) about each channel of the current tunnel, the total number of consumed rows, and the consumer offset.

Assume that you need to synchronize data for the period from 2 pm to 3 pm. If the DescribeTunnel operation obtains the consumer offset 2:30 pm, then half of the data is backed up. If the operation obtains the consumer offset 3:00 pm, then data from the period from 2:00 pm to 3:00 pm is backed up.

Restoring the Files

After backing up the data, you'll need to perform a full restoration or partial restoration as needed to reduce RTO. While restoring your data, you'll also want to take the following measures to optimize restoration performance:

  • Download data from Alibaba Cloud OSS in streaming or multipart mode, and increase the number of concurrent downloads when necessary.
  • Write data to Tablestore through BatchWrite. The following snippet shows the Restore instance code (excluding a few details) that is used to perform the following operations in sequence:
  1. Compute the name of the file to download based on the backup policy.
  2. Read the file from OSS through streaming download.
  3. Write the file to the Restore table in Tablestore through BatchWrite.
public class TunnelRestore {
    private ConfigHelper config;
    private final SyncClient syncClient;
    private final CsvHelper csvHelper;
    private final OSSClient ossClient;

    public TunnelRestore(ConfigHelper config) {
        this.config = config;
        syncClient = new SyncClient(config.getEndpoint(), config.getAccessId(), config.getAccessKey(),
            config.getInstanceName());
        ossClient = new OSSClient(config.getOssEndpoint(), config.getAccessId(), config.getAccessKey());
        csvHelper = new CsvHelper(syncClient, ossClient);
    }

    public void restore(String filename, String tableName) {
        csvHelper.parseStreamRecordsFromCSV(filename, tableName);
    }

    public static void main(String[] args) {
        TunnelRestore restore = new TunnelRestore(new ConfigHelper());
        restore.restore("FullData-1551767131130.csv", "testRestore");
    }
}

Summary

To conclude, this article has described the following items:

  1. The requirements of backup through Tablestore.
  2. The principles of data backup and restoration through the Tunnel Service of Tablestore.
  3. A step-by-step process for how you can develop a data backup and restoration solution based on Tablestore by using real code snippets.
0 0 0
Share on

Alibaba Cloud Storage

57 posts | 12 followers

You may also like

Comments