|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.broker.admin.impl; |
| 20 | + |
| 21 | +import io.swagger.annotations.ApiOperation; |
| 22 | +import io.swagger.annotations.ApiParam; |
| 23 | +import io.swagger.annotations.ApiResponse; |
| 24 | +import io.swagger.annotations.ApiResponses; |
| 25 | +import javax.ws.rs.GET; |
| 26 | +import javax.ws.rs.POST; |
| 27 | +import javax.ws.rs.Path; |
| 28 | +import javax.ws.rs.QueryParam; |
| 29 | +import javax.ws.rs.core.Response; |
| 30 | +import lombok.extern.slf4j.Slf4j; |
| 31 | +import org.apache.pulsar.broker.admin.AdminResource; |
| 32 | +import org.apache.pulsar.broker.web.RestException; |
| 33 | +import org.apache.pulsar.common.migration.MigrationState; |
| 34 | +import org.apache.pulsar.common.util.ObjectMapperFactory; |
| 35 | +import org.apache.pulsar.metadata.coordination.impl.MigrationCoordinator; |
| 36 | +import org.apache.pulsar.metadata.impl.DualMetadataStore; |
| 37 | + |
| 38 | +/** |
| 39 | + * Admin resource for metadata store migration operations. |
| 40 | + */ |
| 41 | +@Slf4j |
| 42 | +public class MetadataMigrationBase extends AdminResource { |
| 43 | + |
| 44 | + @GET |
| 45 | + @Path("/status") |
| 46 | + @ApiOperation(value = "Get current migration status", response = MigrationState.class) |
| 47 | + @ApiResponses(value = { |
| 48 | + @ApiResponse(code = 200, message = "Migration status retrieved successfully"), |
| 49 | + @ApiResponse(code = 500, message = "Internal server error") |
| 50 | + }) |
| 51 | + public MigrationState getStatus() { |
| 52 | + validateSuperUserAccess(); |
| 53 | + |
| 54 | + try { |
| 55 | + var ogr = pulsar().getLocalMetadataStore().get(MigrationState.MIGRATION_FLAG_PATH).get(); |
| 56 | + if (ogr.isPresent()) { |
| 57 | + return ObjectMapperFactory.getMapper().reader().readValue(ogr.get().getValue(), MigrationState.class); |
| 58 | + } else { |
| 59 | + return MigrationState.NOT_STARTED; |
| 60 | + } |
| 61 | + } catch (Exception e) { |
| 62 | + log.error("Failed to get migration status", e); |
| 63 | + throw new RestException(e); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @POST |
| 68 | + @Path("/start") |
| 69 | + @ApiOperation(value = "Start metadata store migration") |
| 70 | + @ApiResponses(value = { |
| 71 | + @ApiResponse(code = 204, message = "Migration started successfully"), |
| 72 | + @ApiResponse(code = 400, message = "Invalid target URL"), |
| 73 | + @ApiResponse(code = 409, message = "Migration already in progress"), |
| 74 | + @ApiResponse(code = 500, message = "Internal server error") |
| 75 | + }) |
| 76 | + public void startMigration( |
| 77 | + @ApiParam(value = "Target metadata store URL", required = true) |
| 78 | + @QueryParam("target") |
| 79 | + String targetUrl) { |
| 80 | + validateSuperUserAccess(); |
| 81 | + |
| 82 | + if (targetUrl == null || targetUrl.trim().isEmpty()) { |
| 83 | + throw new RestException(Response.Status.BAD_REQUEST, "Target URL is required"); |
| 84 | + } |
| 85 | + |
| 86 | + try { |
| 87 | + // Check if metadata store is wrapped with DualMetadataStore |
| 88 | + if (!(pulsar().getLocalMetadataStore() instanceof DualMetadataStore)) { |
| 89 | + throw new RestException(Response.Status.BAD_REQUEST, "Metadata store is not configured for migration. " |
| 90 | + + "Please ensure you're using a supported source metadata store (e.g., ZooKeeper)."); |
| 91 | + } |
| 92 | + |
| 93 | + // Create coordinator |
| 94 | + MigrationCoordinator coordinator = new MigrationCoordinator(pulsar().getLocalMetadataStore(), targetUrl); |
| 95 | + |
| 96 | + // Start migration in background thread |
| 97 | + pulsar().getExecutor().submit(() -> { |
| 98 | + try { |
| 99 | + log.info("Starting metadata migration to: {}", targetUrl); |
| 100 | + coordinator.startMigration(); |
| 101 | + log.info("Metadata migration completed successfully"); |
| 102 | + } catch (Exception e) { |
| 103 | + log.error("Metadata migration failed", e); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + log.info("Migration initiated to target: {}", targetUrl); |
| 108 | + |
| 109 | + } catch (RestException e) { |
| 110 | + throw e; |
| 111 | + } catch (Exception e) { |
| 112 | + log.error("Failed to start migration", e); |
| 113 | + throw new RestException(e); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments