-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature-13429][Remote Logging] Add support for writing task logs to …
…Google Cloud Storage
- Loading branch information
1 parent
69e7449
commit edbcc17
Showing
14 changed files
with
287 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
...mmon/src/main/java/org/apache/dolphinscheduler/common/log/remote/GcsRemoteLogHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.dolphinscheduler.common.log.remote; | ||
|
||
import org.apache.dolphinscheduler.common.constants.Constants; | ||
import org.apache.dolphinscheduler.common.utils.PropertyUtils; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.io.Closeable; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import com.google.auth.oauth2.ServiceAccountCredentials; | ||
import com.google.cloud.storage.Blob; | ||
import com.google.cloud.storage.BlobId; | ||
import com.google.cloud.storage.BlobInfo; | ||
import com.google.cloud.storage.Bucket; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
|
||
@Slf4j | ||
public class GcsRemoteLogHandler implements RemoteLogHandler, Closeable { | ||
|
||
private Storage gcsStorage; | ||
|
||
private String bucketName; | ||
|
||
private String credential; | ||
|
||
private static GcsRemoteLogHandler instance; | ||
|
||
private GcsRemoteLogHandler() { | ||
|
||
} | ||
|
||
public static synchronized GcsRemoteLogHandler getInstance() { | ||
if (instance == null) { | ||
instance = new GcsRemoteLogHandler(); | ||
instance.init(); | ||
} | ||
|
||
return instance; | ||
} | ||
|
||
public void init() { | ||
try { | ||
credential = readCredentials(); | ||
bucketName = readBucketName(); | ||
gcsStorage = buildGcsStorage(credential); | ||
|
||
checkBucketNameExists(bucketName); | ||
} catch (IOException e) { | ||
log.error("GCS Remote Log Handler init failed", e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
try { | ||
if (gcsStorage != null) { | ||
gcsStorage.close(); | ||
} | ||
} catch (Exception e) { | ||
throw new IOException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void sendRemoteLog(String logPath) { | ||
String objectName = RemoteLogUtils.getObjectNameFromLogPath(logPath); | ||
|
||
try { | ||
log.info("send remote log {} to GCS {}", logPath, objectName); | ||
|
||
BlobInfo blobInfo = BlobInfo.newBuilder( | ||
BlobId.of(bucketName, objectName)).build(); | ||
|
||
gcsStorage.create(blobInfo, Files.readAllBytes(Paths.get(logPath))); | ||
} catch (Exception e) { | ||
log.error("error while sending remote log {} to GCS {}", logPath, objectName, e); | ||
} | ||
} | ||
|
||
@Override | ||
public void getRemoteLog(String logPath) { | ||
String objectName = RemoteLogUtils.getObjectNameFromLogPath(logPath); | ||
|
||
try { | ||
log.info("get remote log on GCS {} to {}", objectName, logPath); | ||
|
||
File dstFile = new File(logPath); | ||
if (dstFile.isDirectory()) { | ||
Files.delete(dstFile.toPath()); | ||
} else { | ||
Files.createDirectories(dstFile.getParentFile().toPath()); | ||
} | ||
|
||
Blob blob = gcsStorage.get(BlobId.of(bucketName, objectName)); | ||
blob.downloadTo(Paths.get(logPath)); | ||
} catch (Exception e) { | ||
log.error("error while getting remote log on GCS {} to {}", objectName, logPath, e); | ||
} | ||
} | ||
|
||
protected Storage buildGcsStorage(String credential) throws IOException { | ||
return StorageOptions.newBuilder() | ||
.setCredentials(ServiceAccountCredentials.fromStream( | ||
Files.newInputStream(Paths.get(credential)))) | ||
.build() | ||
.getService(); | ||
} | ||
|
||
protected String readCredentials() { | ||
return PropertyUtils.getString(Constants.REMOTE_LOGGING_GCS_CREDENTIAL); | ||
} | ||
|
||
protected String readBucketName() { | ||
return PropertyUtils.getString(Constants.REMOTE_LOGGING_GCS_BUCKET_NAME); | ||
} | ||
|
||
public void checkBucketNameExists(String bucketName) { | ||
if (StringUtils.isBlank(bucketName)) { | ||
throw new IllegalArgumentException(Constants.REMOTE_LOGGING_GCS_BUCKET_NAME + " is blank"); | ||
} | ||
|
||
boolean exist = false; | ||
for (Bucket bucket : gcsStorage.list().iterateAll()) { | ||
if (bucketName.equals(bucket.getName())) { | ||
exist = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!exist) { | ||
throw new IllegalArgumentException( | ||
"bucketName: " + bucketName + " is not exists, you need to create them by yourself"); | ||
} else { | ||
log.info("bucketName: {} has been found", bucketName); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/LogUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.dolphinscheduler.common.utils; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import org.slf4j.LoggerFactory; | ||
|
||
import ch.qos.logback.classic.LoggerContext; | ||
|
||
@Slf4j | ||
public class LogUtils { | ||
|
||
public static String getLocalLogBaseDir() { | ||
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); | ||
return loggerContext.getProperty("log.base.ctx"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.