|
15 | 15 | */
|
16 | 16 | package io.cdap.cdap;
|
17 | 17 |
|
| 18 | +import com.google.gson.Gson; |
18 | 19 | import com.google.inject.AbstractModule;
|
19 | 20 | import com.google.inject.Guice;
|
20 | 21 | import com.google.inject.Injector;
|
|
23 | 24 | import io.cdap.cdap.api.metrics.MetricsCollectionService;
|
24 | 25 | import io.cdap.cdap.common.conf.CConfiguration;
|
25 | 26 | import io.cdap.cdap.common.guice.ConfigModule;
|
26 |
| -import io.cdap.cdap.common.guice.DFSLocationModule; |
27 |
| -import io.cdap.cdap.common.guice.IOModule; |
28 |
| -import io.cdap.cdap.common.guice.InMemoryDiscoveryModule; |
29 |
| -import io.cdap.cdap.common.guice.RemoteAuthenticatorModules; |
30 | 27 | import io.cdap.cdap.common.metrics.NoOpMetricsCollectionService;
|
31 |
| -import io.cdap.cdap.data.runtime.ConstantTransactionSystemClient; |
32 |
| -import io.cdap.cdap.data.runtime.DataSetsModules; |
33 | 28 | import io.cdap.cdap.data.runtime.StorageModule;
|
34 |
| -import io.cdap.cdap.data.runtime.SystemDatasetRuntimeModule; |
35 |
| -import io.cdap.cdap.security.auth.context.AuthenticationContextModules; |
36 |
| -import io.cdap.cdap.security.spi.authorization.AccessEnforcer; |
37 |
| -import io.cdap.cdap.security.spi.authorization.NoOpAccessController; |
38 | 29 | import io.cdap.cdap.store.NamespaceTable;
|
| 30 | +import java.nio.charset.StandardCharsets; |
39 | 31 | import java.util.ArrayList;
|
40 | 32 | import java.util.Arrays;
|
41 | 33 | import java.util.List;
|
42 | 34 | import io.cdap.cdap.spi.data.transaction.TransactionRunner;
|
43 | 35 | import io.cdap.cdap.spi.data.transaction.TransactionRunners;
|
44 |
| -import org.apache.tephra.TransactionSystemClient; |
45 | 36 | import org.slf4j.Logger;
|
46 | 37 | import org.slf4j.LoggerFactory;
|
47 | 38 | import io.cdap.cdap.proto.NamespaceMeta;
|
| 39 | +import com.google.cloud.storage.BlobId; |
| 40 | +import com.google.cloud.storage.BlobInfo; |
| 41 | +import com.google.cloud.storage.Storage; |
| 42 | +import com.google.cloud.storage.StorageOptions; |
48 | 43 |
|
49 | 44 | public class ExportJobMain
|
50 | 45 | {
|
51 | 46 | private final static Logger LOG = LoggerFactory.getLogger(ExportJobMain.class);
|
52 |
| - public void exportNamespaces() { |
53 |
| - CConfiguration cConf = CConfiguration.create(); |
54 |
| - List<Module> modules = new ArrayList<>(Arrays.asList( |
55 |
| - new ConfigModule(cConf), |
56 |
| - new StorageModule(), |
57 |
| - new AbstractModule() { |
58 |
| - @Override |
59 |
| - protected void configure() { |
60 |
| - bind(MetricsCollectionService.class).to(NoOpMetricsCollectionService.class) |
61 |
| - .in(Scopes.SINGLETON); |
62 |
| - } |
63 |
| - } |
64 |
| - )); |
65 |
| - Injector injector = Guice.createInjector(modules); |
66 |
| - TransactionRunner transactionRunner = injector.getInstance(TransactionRunner.class); |
| 47 | + private static final Gson GSON = new Gson(); |
| 48 | + public void exportNamespaces(TransactionRunner transactionRunner, String bucketName, Storage gcsClient) { |
67 | 49 | LOG.debug("Starting export of namespaces");
|
68 |
| - TransactionRunners.run(transactionRunner, context -> { |
69 |
| - NamespaceTable namespaceTable = new NamespaceTable(context); |
70 |
| - List<NamespaceMeta> namespaces = namespaceTable.list(); |
71 |
| - LOG.debug("Found {} namespaces: {}", namespaces.size(), namespaces); |
72 |
| - }); |
| 50 | + |
| 51 | + try { |
| 52 | + TransactionRunners.run(transactionRunner, context -> { |
| 53 | + NamespaceTable namespaceTable = new NamespaceTable(context); |
| 54 | + List<NamespaceMeta> namespaces = namespaceTable.list(); |
| 55 | + LOG.info("Found {} namespaces to export.", namespaces.size()); |
| 56 | + |
| 57 | + for (NamespaceMeta namespace : namespaces) { |
| 58 | + String namespaceId = namespace.getName(); |
| 59 | + LOG.debug("Processing namespace '{}'...", namespaceId); |
| 60 | + |
| 61 | + String namespaceJson = GSON.toJson(namespace); |
| 62 | + |
| 63 | + String gcsObjectPath = String.format("cdap/namespaces/%s/namespaceMeta", namespaceId); |
| 64 | + |
| 65 | + // Prepare the object for upload. |
| 66 | + BlobId blobId = BlobId.of(bucketName, gcsObjectPath); |
| 67 | + BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("application/json").build(); |
| 68 | + |
| 69 | + // Upload the JSON content as bytes. |
| 70 | + gcsClient.create(blobInfo, namespaceJson.getBytes(StandardCharsets.UTF_8)); |
| 71 | + |
| 72 | + LOG.info("Successfully exported namespace '{}' to gs://{}/{}", |
| 73 | + namespaceId, bucketName, gcsObjectPath); |
| 74 | + } |
| 75 | + }, Exception.class); |
| 76 | + } catch (Exception e) { |
| 77 | + LOG.error("Failed to export namespaces due to an unexpected transaction error.", e); |
| 78 | + throw new RuntimeException("Namespace export failed", e); |
| 79 | + } |
73 | 80 | LOG.debug("Finished exporting namespaces.");
|
74 | 81 | }
|
75 | 82 | public static void main( String[] args )
|
76 | 83 | {
|
77 |
| - LOG.debug("Args: {}", args); |
| 84 | + LOG.debug("Args: {}", args); |
| 85 | + CConfiguration cConf = CConfiguration.create(); |
| 86 | + List<Module> modules = new ArrayList<>(Arrays.asList( |
| 87 | + new ConfigModule(cConf), |
| 88 | + new StorageModule(), |
| 89 | + new AbstractModule() { |
| 90 | + @Override |
| 91 | + protected void configure() { |
| 92 | + bind(MetricsCollectionService.class).to(NoOpMetricsCollectionService.class) |
| 93 | + .in(Scopes.SINGLETON); |
| 94 | + } |
| 95 | + } |
| 96 | + )); |
| 97 | + Injector injector = Guice.createInjector(modules); |
| 98 | + TransactionRunner transactionRunner = injector.getInstance(TransactionRunner.class); |
| 99 | + String gcsBucket = args[0]; |
| 100 | + Storage gcsClient; |
| 101 | + try { |
| 102 | + gcsClient = StorageOptions.getDefaultInstance().getService(); |
| 103 | + LOG.info("Successfully initialized Google Cloud Storage client."); |
| 104 | + } catch (Exception e) { |
| 105 | + LOG.error("Failed to initialize GCS client. Please check authentication.", e); |
| 106 | + return; |
| 107 | + } |
78 | 108 | ExportJobMain exportJob = new ExportJobMain();
|
79 |
| - exportJob.exportNamespaces(); |
| 109 | + exportJob.exportNamespaces(transactionRunner, gcsBucket, gcsClient); |
80 | 110 | System.out.println("Job finished.");
|
81 | 111 | }
|
82 | 112 | }
|
0 commit comments