|
| 1 | +/* |
| 2 | + * Copyright © 2025 Cask Data, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package io.cdap.cdap; |
| 17 | + |
| 18 | +import com.google.gson.Gson; |
| 19 | +import com.google.inject.AbstractModule; |
| 20 | +import com.google.inject.Guice; |
| 21 | +import com.google.inject.Injector; |
| 22 | +import com.google.inject.Module; |
| 23 | +import com.google.inject.Scopes; |
| 24 | +import io.cdap.cdap.api.metrics.MetricsCollectionService; |
| 25 | +import io.cdap.cdap.common.conf.CConfiguration; |
| 26 | +import io.cdap.cdap.common.guice.ConfigModule; |
| 27 | +import io.cdap.cdap.common.metrics.NoOpMetricsCollectionService; |
| 28 | +import io.cdap.cdap.data.runtime.StorageModule; |
| 29 | +import io.cdap.cdap.store.NamespaceTable; |
| 30 | +import java.nio.charset.StandardCharsets; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.List; |
| 34 | +import io.cdap.cdap.spi.data.transaction.TransactionRunner; |
| 35 | +import io.cdap.cdap.spi.data.transaction.TransactionRunners; |
| 36 | +import org.slf4j.Logger; |
| 37 | +import org.slf4j.LoggerFactory; |
| 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; |
| 43 | + |
| 44 | +public class ExportJobMainTemp |
| 45 | +{ |
| 46 | + private final static Logger LOG = LoggerFactory.getLogger(ExportJobMainTemp.class); |
| 47 | + private static final Gson GSON = new Gson(); |
| 48 | + public void exportNamespaces(TransactionRunner transactionRunner, String bucketName, Storage gcsClient) { |
| 49 | + // CConfiguration cConf = CConfiguration.create(); |
| 50 | + // List<Module> modules = new ArrayList<>(Arrays.asList( |
| 51 | + // new ConfigModule(cConf), |
| 52 | + // new StorageModule(), |
| 53 | + // new AbstractModule() { |
| 54 | + // @Override |
| 55 | + // protected void configure() { |
| 56 | + // bind(MetricsCollectionService.class).to(NoOpMetricsCollectionService.class) |
| 57 | + // .in(Scopes.SINGLETON); |
| 58 | + // } |
| 59 | + // } |
| 60 | + // )); |
| 61 | + // Injector injector = Guice.createInjector(modules); |
| 62 | + // TransactionRunner transactionRunner = injector.getInstance(TransactionRunner.class); |
| 63 | + LOG.debug("Starting export of namespaces"); |
| 64 | + |
| 65 | + try { |
| 66 | + TransactionRunners.run(transactionRunner, context -> { |
| 67 | + NamespaceTable namespaceTable = new NamespaceTable(context); |
| 68 | + List<NamespaceMeta> namespaces = namespaceTable.list(); |
| 69 | + LOG.info("Found {} namespaces to export.", namespaces.size()); |
| 70 | + |
| 71 | + // Loop through each namespace and upload its metadata. |
| 72 | + for (NamespaceMeta namespace : namespaces) { |
| 73 | + String namespaceId = namespace.getName(); |
| 74 | + LOG.debug("Processing namespace '{}'...", namespaceId); |
| 75 | + |
| 76 | + // Convert the NamespaceMeta object to a JSON string. |
| 77 | + String namespaceJson = GSON.toJson(namespace); |
| 78 | + |
| 79 | + // Define the full path for the object in GCS. |
| 80 | + String gcsObjectPath = String.format("cdap/namespaces/%s/namespaceMeta", namespaceId); |
| 81 | + |
| 82 | + // Prepare the object for upload. |
| 83 | + BlobId blobId = BlobId.of(bucketName, gcsObjectPath); |
| 84 | + BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("application/json").build(); |
| 85 | + |
| 86 | + // Upload the JSON content as bytes. |
| 87 | + gcsClient.create(blobInfo, namespaceJson.getBytes(StandardCharsets.UTF_8)); |
| 88 | + |
| 89 | + LOG.info("Successfully exported namespace '{}' to gs://{}/{}", |
| 90 | + namespaceId, bucketName, gcsObjectPath); |
| 91 | + } |
| 92 | + }, Exception.class); |
| 93 | + } catch (Exception e) { |
| 94 | + LOG.error("Failed to export namespaces due to an unexpected transaction error.", e); |
| 95 | + // In a real K8s job, you might want the pod to fail. Throwing a RuntimeException |
| 96 | + // will cause the Java process to exit with a non-zero status code. |
| 97 | + throw new RuntimeException("Namespace export failed", e); |
| 98 | + } |
| 99 | + // TransactionRunners.run(transactionRunner, context -> { |
| 100 | + // NamespaceTable namespaceTable = new NamespaceTable(context); |
| 101 | + // List<NamespaceMeta> namespaces = namespaceTable.list(); |
| 102 | + // LOG.debug("Found {} namespaces: {}", namespaces.size(), namespaces); |
| 103 | + // }); |
| 104 | + LOG.debug("Finished exporting namespaces."); |
| 105 | + } |
| 106 | + public static void main( String[] args ) |
| 107 | + { |
| 108 | + LOG.debug("Args: {}", args); |
| 109 | + CConfiguration cConf = CConfiguration.create(); |
| 110 | + List<Module> modules = new ArrayList<>(Arrays.asList( |
| 111 | + new ConfigModule(cConf), |
| 112 | + new StorageModule(), |
| 113 | + new AbstractModule() { |
| 114 | + @Override |
| 115 | + protected void configure() { |
| 116 | + bind(MetricsCollectionService.class).to(NoOpMetricsCollectionService.class) |
| 117 | + .in(Scopes.SINGLETON); |
| 118 | + } |
| 119 | + } |
| 120 | + )); |
| 121 | + Injector injector = Guice.createInjector(modules); |
| 122 | + TransactionRunner transactionRunner = injector.getInstance(TransactionRunner.class); |
| 123 | + String gcsBucket = args[0]; |
| 124 | + Storage gcsClient; |
| 125 | + try { |
| 126 | + gcsClient = StorageOptions.getDefaultInstance().getService(); |
| 127 | + LOG.info("Successfully initialized Google Cloud Storage client."); |
| 128 | + } catch (Exception e) { |
| 129 | + LOG.error("Failed to initialize GCS client. Please check authentication.", e); |
| 130 | + return; |
| 131 | + } |
| 132 | + ExportJobMainTemp exportJob = new ExportJobMainTemp(); |
| 133 | + exportJob.exportNamespaces(transactionRunner, gcsBucket, gcsClient); |
| 134 | + System.out.println("Job finished."); |
| 135 | + } |
| 136 | +} |
0 commit comments