3636import java .util .List ;
3737import java .util .concurrent .ExecutionException ;
3838import java .util .concurrent .TimeUnit ;
39+ import java .util .concurrent .atomic .AtomicBoolean ;
3940
4041import javax .inject .Inject ;
42+ import javax .validation .constraints .NotNull ;
4143
4244import org .apache .commons .io .FileUtils ;
4345import org .apache .hadoop .hdds .conf .OzoneConfiguration ;
4749import org .apache .hadoop .hdds .scm .container .ContainerInfo ;
4850import org .apache .hadoop .hdds .scm .container .common .helpers .ContainerWithPipeline ;
4951import org .apache .hadoop .hdds .scm .ha .InterSCMGrpcClient ;
50- import org .apache .hadoop .hdds .scm .ha .SCMHAUtils ;
5152import org .apache .hadoop .hdds .scm .ha .SCMSnapshotDownloader ;
5253import org .apache .hadoop .hdds .scm .pipeline .Pipeline ;
5354import org .apache .hadoop .hdds .scm .protocol .StorageContainerLocationProtocol ;
5758import org .apache .hadoop .hdds .utils .db .RocksDBCheckpoint ;
5859import org .apache .hadoop .hdfs .web .URLConnectionFactory ;
5960import org .apache .hadoop .ozone .ClientVersion ;
61+ import org .apache .hadoop .ozone .recon .ReconContext ;
6062import org .apache .hadoop .ozone .recon .ReconUtils ;
6163import org .apache .hadoop .ozone .recon .scm .ReconStorageConfig ;
6264import org .apache .hadoop .ozone .recon .security .ReconCertificateClient ;
@@ -82,13 +84,15 @@ public class StorageContainerServiceProviderImpl
8284 private URLConnectionFactory connectionFactory ;
8385 private ReconUtils reconUtils ;
8486 private ReconStorageConfig reconStorage ;
87+ private ReconContext reconContext ;
8588
8689 @ Inject
8790 public StorageContainerServiceProviderImpl (
8891 StorageContainerLocationProtocol scmClient ,
8992 ReconUtils reconUtils ,
9093 OzoneConfiguration configuration ,
91- ReconStorageConfig reconStorage ) {
94+ ReconStorageConfig reconStorage ,
95+ ReconContext reconContext ) {
9296
9397 int connectionTimeout = (int ) configuration .getTimeDuration (
9498 OZONE_RECON_SCM_CONNECTION_TIMEOUT ,
@@ -123,6 +127,7 @@ public StorageContainerServiceProviderImpl(
123127 this .scmClient = scmClient ;
124128 this .configuration = configuration ;
125129 this .reconStorage = reconStorage ;
130+ this .reconContext = reconContext ;
126131 }
127132
128133 @ Override
@@ -178,59 +183,78 @@ public DBCheckpoint getSCMDBSnapshot() {
178183 String snapshotFileName = RECON_SCM_SNAPSHOT_DB + "_" +
179184 System .currentTimeMillis ();
180185 File targetFile = new File (scmSnapshotDBParentDir , snapshotFileName +
181- ".tar" );
182-
186+ ".tar" );
183187 try {
184- if (!SCMHAUtils .isSCMHAEnabled (configuration )) {
185- SecurityUtil .doAsLoginUser (() -> {
186- try (InputStream inputStream = reconUtils .makeHttpCall (
187- connectionFactory , getScmDBSnapshotUrl (),
188- isOmSpnegoEnabled ()).getInputStream ()) {
189- FileUtils .copyInputStreamToFile (inputStream , targetFile );
190- }
191- return null ;
192- });
193- LOG .info ("Downloaded SCM Snapshot from SCM" );
194- } else {
188+ try {
195189 List <String > ratisRoles = scmClient .getScmInfo ().getRatisPeerRoles ();
196- for (String ratisRole : ratisRoles ) {
190+ for (String ratisRole : ratisRoles ) {
197191 String [] role = ratisRole .split (":" );
198- if (role [2 ].equals (RaftProtos .RaftPeerRole .LEADER .toString ())) {
199- String hostAddress = role [4 ].trim ();
200- int grpcPort = configuration .getInt (
201- ScmConfigKeys .OZONE_SCM_GRPC_PORT_KEY ,
202- ScmConfigKeys .OZONE_SCM_GRPC_PORT_DEFAULT );
203-
204- SecurityConfig secConf = new SecurityConfig (configuration );
205- SCMSecurityProtocolClientSideTranslatorPB scmSecurityClient =
206- getScmSecurityClientWithMaxRetry (
207- configuration , getCurrentUser ());
208- try (ReconCertificateClient certClient =
209- new ReconCertificateClient (
210- secConf , scmSecurityClient , reconStorage , null , null );
211- SCMSnapshotDownloader downloadClient = new InterSCMGrpcClient (
212- hostAddress , grpcPort , configuration , certClient )) {
213- downloadClient .download (targetFile .toPath ()).get ();
214- } catch (ExecutionException | InterruptedException e ) {
215- LOG .error ("Rocks DB checkpoint downloading failed" , e );
216- throw new IOException (e );
192+ // This explicit role length check is to support older versions where we cannot change the default value
193+ // without breaking backward compatibility during upgrade, because if Ratis is not enabled then the roles
194+ // command output is generated outside of Ratis. It will not have the Ratis terminologies.
195+ if (role .length > 2 ) {
196+ if (role [2 ].equals (RaftProtos .RaftPeerRole .LEADER .toString ())) {
197+ String hostAddress = role [4 ].trim ();
198+ int grpcPort = configuration .getInt (
199+ ScmConfigKeys .OZONE_SCM_GRPC_PORT_KEY ,
200+ ScmConfigKeys .OZONE_SCM_GRPC_PORT_DEFAULT );
201+
202+ SecurityConfig secConf = new SecurityConfig (configuration );
203+ SCMSecurityProtocolClientSideTranslatorPB scmSecurityClient =
204+ getScmSecurityClientWithMaxRetry (
205+ configuration , getCurrentUser ());
206+ try (ReconCertificateClient certClient =
207+ new ReconCertificateClient (
208+ secConf , scmSecurityClient , reconStorage , null , null );
209+ SCMSnapshotDownloader downloadClient = new InterSCMGrpcClient (
210+ hostAddress , grpcPort , configuration , certClient )) {
211+ downloadClient .download (targetFile .toPath ()).get ();
212+ } catch (ExecutionException | InterruptedException e ) {
213+ LOG .error ("Rocks DB checkpoint downloading failed: {}" , e );
214+ throw new IOException (e );
215+ }
216+ LOG .info ("Downloaded SCM Snapshot from Leader SCM" );
217+ break ;
217218 }
218- LOG .info ("Downloaded SCM Snapshot from Leader SCM" );
219+ } else {
220+ fetchSCMDBSnapshotUsingHttpClient (targetFile );
221+ LOG .info ("Downloaded SCM Snapshot from SCM" );
219222 break ;
220223 }
221224 }
225+ } catch (Throwable throwable ) {
226+ LOG .error ("Unexpected runtime error while downloading SCM Rocks DB snapshot/checkpoint : {}" , throwable );
227+ throw throwable ;
222228 }
223- Path untarredDbDir = Paths .get (scmSnapshotDBParentDir .getAbsolutePath (),
224- snapshotFileName );
225- reconUtils .untarCheckpointFile (targetFile , untarredDbDir );
226- FileUtils .deleteQuietly (targetFile );
227- return new RocksDBCheckpoint (untarredDbDir );
228- } catch (IOException e ) {
229- LOG .error ("Unable to obtain SCM DB Snapshot. " , e );
229+ return getRocksDBCheckpoint (snapshotFileName , targetFile );
230+ } catch (Throwable e ) {
231+ reconContext .updateHealthStatus (new AtomicBoolean (false ));
232+ reconContext .getErrors ().add (ReconContext .ErrorCode .GET_SCM_DB_SNAPSHOT_FAILED );
233+ LOG .error ("Unable to obtain SCM DB Snapshot: {} " , e );
230234 }
231235 return null ;
232236 }
233237
238+ private void fetchSCMDBSnapshotUsingHttpClient (File targetFile ) throws IOException {
239+ SecurityUtil .doAsLoginUser (() -> {
240+ try (InputStream inputStream = reconUtils .makeHttpCall (
241+ connectionFactory , getScmDBSnapshotUrl (),
242+ isOmSpnegoEnabled ()).getInputStream ()) {
243+ FileUtils .copyInputStreamToFile (inputStream , targetFile );
244+ }
245+ return null ;
246+ });
247+ }
248+
249+ @ NotNull
250+ private RocksDBCheckpoint getRocksDBCheckpoint (String snapshotFileName , File targetFile ) throws IOException {
251+ Path untarredDbDir = Paths .get (scmSnapshotDBParentDir .getAbsolutePath (),
252+ snapshotFileName );
253+ reconUtils .untarCheckpointFile (targetFile , untarredDbDir );
254+ FileUtils .deleteQuietly (targetFile );
255+ return new RocksDBCheckpoint (untarredDbDir );
256+ }
257+
234258 @ Override
235259 public List <ContainerInfo > getListOfContainers (
236260 long startContainerID , int count , HddsProtos .LifeCycleState state )
0 commit comments