Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ public static String zipString(String text) {
}

public static String unzipString(String zipString) {
byte[] decode = Base64.getDecoder().decode(zipString);
byte[] decode;
try {
decode = Base64.getDecoder().decode(zipString);
} catch (IllegalArgumentException e) {
return null;
}
Inflater inflater = new Inflater();
inflater.setInput(decode);
byte[] bytes = new byte[256];
Expand All @@ -71,4 +76,27 @@ public static String unzipString(String zipString) {
}
return outputStream.toString();
}

/** Returns plain text, repeatedly decoding when the input is compressed multiple times. */
public static String toPlainText(String text) {
if (StringUtils.isBlank(text)) {
return text;
}
String current = text;
while (true) {
String decoded = unzipString(current);
if (decoded == null) {
return current;
}
current = decoded;
}
}

/** Stores SQL/conf text as a single compressed blob regardless of input encoding. */
public static String compressForStorage(String text) {
if (StringUtils.isBlank(text)) {
return "";
}
return zipString(toPlainText(text));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@
FlinkApplication app = new FlinkApplication();
app.setId(request.getId());
app.setTeamId(request.getTeamId());
app.setRestoreOrTriggerSavepoint(request.getRestoreOrTriggerSavepoint());
app.setRestoreOrTriggerSavepoint(
request.getRestoreOrTriggerSavepoint() != null ? request.getRestoreOrTriggerSavepoint() : false);

Check warning on line 90 in streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/assembler/FlinkApplicationAssembler.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the unnecessary boolean literal.

See more on https://sonarcloud.io/project/issues?id=apache_incubator-streampark&issues=AaA3SPGo3ySR3UK6slL9&open=AaA3SPGo3ySR3UK6slL9&pullRequest=4507
app.setSavepointPath(request.getSavepointPath());
app.setAllowNonRestored(request.getAllowNonRestored());
app.setAllowNonRestored(
request.getAllowNonRestored() != null ? request.getAllowNonRestored() : false);

Check warning on line 93 in streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/assembler/FlinkApplicationAssembler.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the unnecessary boolean literal.

See more on https://sonarcloud.io/project/issues?id=apache_incubator-streampark&issues=AaA3SPGo3ySR3UK6slL-&open=AaA3SPGo3ySR3UK6slL-&pullRequest=4507
return app;
}

Expand All @@ -99,9 +101,10 @@
FlinkApplication app = new FlinkApplication();
app.setId(request.getId());
app.setTeamId(request.getTeamId());
app.setRestoreOrTriggerSavepoint(request.getRestoreOrTriggerSavepoint());
app.setDrain(request.getDrain());
app.setNativeFormat(request.getNativeFormat());
app.setRestoreOrTriggerSavepoint(
request.getRestoreOrTriggerSavepoint() != null ? request.getRestoreOrTriggerSavepoint() : false);

Check warning on line 105 in streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/assembler/FlinkApplicationAssembler.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the unnecessary boolean literal.

See more on https://sonarcloud.io/project/issues?id=apache_incubator-streampark&issues=AaA3SPGo3ySR3UK6slL_&open=AaA3SPGo3ySR3UK6slL_&pullRequest=4507
app.setDrain(request.getDrain() != null ? request.getDrain() : false);

Check warning on line 106 in streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/assembler/FlinkApplicationAssembler.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the unnecessary boolean literal.

See more on https://sonarcloud.io/project/issues?id=apache_incubator-streampark&issues=AaA3SPGo3ySR3UK6slMA&open=AaA3SPGo3ySR3UK6slMA&pullRequest=4507
app.setNativeFormat(request.getNativeFormat() != null ? request.getNativeFormat() : false);

Check warning on line 107 in streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/assembler/FlinkApplicationAssembler.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the unnecessary boolean literal.

See more on https://sonarcloud.io/project/issues?id=apache_incubator-streampark&issues=AaA3SPGo3ySR3UK6slMB&open=AaA3SPGo3ySR3UK6slMB&pullRequest=4507
app.setSavepointPath(request.getSavepointPath());
return app;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.apache.streampark.console.core.bean.Dependency;
import org.apache.streampark.console.core.enums.ChangeTypeEnum;

import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
Expand Down Expand Up @@ -85,7 +87,7 @@
}

public void decode() {
this.setSql(DeflaterUtils.unzipString(this.sql));
this.setSql(DeflaterUtils.toPlainText(this.sql));
}

public void setToApplication(FlinkApplication application) {
Expand All @@ -105,8 +107,17 @@
}

public ChangeTypeEnum checkChange(FlinkSql target) {
if (target == null) {
return ChangeTypeEnum.NONE;
}
// 1) determine if sql statement has changed
boolean sqlDifference = !this.getSql().trim().equals(target.getSql().trim());
String sourceSql =
StringUtils.trimToEmpty(DeflaterUtils.toPlainText(this.getSql()));
String targetSql =
target.getSql() == null
? sourceSql
: StringUtils.trimToEmpty(DeflaterUtils.toPlainText(target.getSql()));
boolean sqlDifference = !sourceSql.equals(targetSql);

Check warning on line 120 in streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/entity/FlinkSql.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename "sqlDifference" which hides the field declared at line 64.

See more on https://sonarcloud.io/project/issues?id=apache_incubator-streampark&issues=AaA3SPQ-3ySR3UK6slMC&open=AaA3SPQ-3ySR3UK6slMC&pullRequest=4507
// 2) determine if dependency has changed
Dependency thisDependency = Dependency.toDependency(this.getDependency());
Dependency targetDependency = Dependency.toDependency(target.getDependency());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public class EnvInitializer implements ApplicationRunner {
"^streampark-flink-shims_flink-(1\\.1[7-9]|1\\.2[0-9]|2\\.[0-3])-(.*).jar$",
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

private static final Pattern PATTERN_FLINK_SHIMS_BASE_JAR = Pattern.compile(
"^streampark-flink-shims-base(-v2)?-(.*)\\.jar$",
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

@SneakyThrows
@Override
public void run(ApplicationArguments args) throws Exception {
Expand Down Expand Up @@ -184,6 +188,10 @@ private void uploadShimsJar(Workspace workspace, FsOperator fsOperator) {
String appShims = workspace.APP_SHIMS();
fsOperator.delete(appShims);

File[] shimsBaseJars =
WebUtils.getAppLibDir().listFiles(
pathname -> pathname.getName().matches(PATTERN_FLINK_SHIMS_BASE_JAR.pattern()));

for (File file : shims) {
Matcher matcher = PATTERN_FLINK_SHIMS_JAR.matcher(file.getName());
if (matcher.matches()) {
Expand All @@ -192,10 +200,26 @@ private void uploadShimsJar(Workspace workspace, FsOperator fsOperator) {
fsOperator.mkdirs(shimsPath);
log.info("load shims:{} to {}", file.getName(), shimsPath);
fsOperator.upload(file.getAbsolutePath(), shimsPath);
if (isFlink2MajorVersion(version) && shimsBaseJars != null) {
uploadShimsBaseJars(fsOperator, shimsBaseJars, shimsPath);
}
}
}
}

private static boolean isFlink2MajorVersion(String majorVersion) {
int dot = majorVersion.indexOf('.');
String major = dot < 0 ? majorVersion : majorVersion.substring(0, dot);
return "2".equals(major);
}

private void uploadShimsBaseJars(FsOperator fsOperator, File[] shimsBaseJars, String shimsPath) {
for (File baseJar : shimsBaseJars) {
log.info("load shims base:{} to {}", baseJar.getName(), shimsPath);
fsOperator.upload(baseJar.getAbsolutePath(), shimsPath);
}
}

public void checkFlinkEnv(StorageType storageType, FlinkEnv flinkEnv) throws IOException {
String flinkLocalHome = flinkEnv.getFlinkHome();
if (StringUtils.isBlank(flinkLocalHome)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public void cancel(FlinkApplication appParam) throws Exception {
applicationLog.setClusterId(application.getClusterId());
applicationLog.setUserId(ServiceHelper.getUserId());

if (appParam.getRestoreOrTriggerSavepoint()) {
if (Boolean.TRUE.equals(appParam.getRestoreOrTriggerSavepoint())) {
FlinkAppHttpWatcher.addSavepoint(application.getId());
application.setOptionState(OptionStateEnum.SAVEPOINTING.getValue());
} else {
Expand All @@ -280,7 +280,7 @@ public void cancel(FlinkApplication appParam) throws Exception {

// infer savepoint
String customSavepoint = null;
if (appParam.getRestoreOrTriggerSavepoint()) {
if (Boolean.TRUE.equals(appParam.getRestoreOrTriggerSavepoint())) {
customSavepoint = appParam.getSavepointPath();
if (StringUtils.isBlank(customSavepoint)) {
customSavepoint = savepointService.getSavePointPath(appParam);
Expand Down Expand Up @@ -315,10 +315,10 @@ public void cancel(FlinkApplication appParam) throws Exception {
properties,
new JobClientTarget(clusterId, application.getJobId(), namespace),
new SavepointCancelOptions(
appParam.getRestoreOrTriggerSavepoint(),
appParam.getDrain(),
Boolean.TRUE.equals(appParam.getRestoreOrTriggerSavepoint()),
Boolean.TRUE.equals(appParam.getDrain()),
customSavepoint,
appParam.getNativeFormat()));
Boolean.TRUE.equals(appParam.getNativeFormat())));

final Date triggerTime = new Date();
CompletableFuture<CancelResponse> cancelFuture =
Expand All @@ -344,7 +344,7 @@ public void cancel(FlinkApplication appParam) throws Exception {
application.setState(FlinkAppStateEnum.FAILED.getValue());
updateById(application);

if (appParam.getRestoreOrTriggerSavepoint()) {
if (Boolean.TRUE.equals(appParam.getRestoreOrTriggerSavepoint())) {
savepointService.expire(application.getId());
}
// re-tracking flink job on kubernetes and logging exception
Expand Down Expand Up @@ -436,10 +436,8 @@ public void start(FlinkApplication appParam, boolean auto) throws Exception {
String flinkUserJar = userJarAndAppConf.t1;
String appConf = userJarAndAppConf.t2;

BuildResult buildResult = buildPipeline.getBuildResult();
if (FlinkDeployMode.YARN_APPLICATION == application.getDeployModeEnum()) {
buildResult = new ShadedBuildResponse(null, flinkUserJar, true);
}
BuildResult buildResult =
resolveBuildResultForSubmit(application, buildPipeline.getBuildResult(), flinkUserJar);

// Get the args after placeholder replacement
String args =
Expand Down Expand Up @@ -637,6 +635,41 @@ private void starting(FlinkApplication application) {
updateById(application);
}

private BuildResult resolveBuildResultForSubmit(
FlinkApplication application,
BuildResult buildResult,
String flinkUserJar) {
if (FlinkDeployMode.YARN_APPLICATION == application.getDeployModeEnum()) {
return new ShadedBuildResponse(null, flinkUserJar, true);
}
if (!(buildResult instanceof ShadedBuildResponse)) {
return buildResult;
}
ShadedBuildResponse shaded = (ShadedBuildResponse) buildResult;
if (StringUtils.isNotBlank(shaded.shadedJarPath())) {
return shaded;
}
String workspace =
StringUtils.defaultIfBlank(shaded.workspacePath(), application.getLocalAppHome());
String jarPath =
workspace
+ "/streampark-flinkjob_"
+ application.getJobName().replaceAll("\\s+", "_")
+ ".jar";
File jarFile = new File(jarPath);
if (!jarFile.isFile()
&& application.isUploadResource()
&& StringUtils.isNotBlank(application.getJar())) {
jarFile = new File(workspace, application.getJar());
}
ApiAlertException.throwIfTrue(
!jarFile.isFile(),
"[StreamPark] Shaded jar not found at "
+ jarPath
+ ", please rebuild the application.");
return new ShadedBuildResponse(workspace, jarPath, shaded.pass());
}

private Tuple2<String, String> getUserJarAndAppConf(
FlinkEnv flinkEnv, FlinkApplication application) {
FlinkDeployMode deployModeEnum = application.getDeployModeEnum();
Expand Down Expand Up @@ -835,7 +868,7 @@ private void doAbort(Long id) {
}

private String getSavepointPath(FlinkApplication appParam) {
if (appParam.getRestoreOrTriggerSavepoint()) {
if (Boolean.TRUE.equals(appParam.getRestoreOrTriggerSavepoint())) {
if (StringUtils.isBlank(appParam.getSavepointPath())) {
FlinkSavepoint savepoint = savepointService.getLatest(appParam.getId());
if (savepoint != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public void onStart(PipelineSnapshot snapshot) {
app.getJar(),
app.getAppHome(),
app.getAppLib(),
app.getDistHome(),
app.isUploadResource() ? null : app.getDistHome(),
app.getFsOperator(),
appUploads,
app.isUploadResource(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ private void updateFlinkClusterForNonRemoteModes(
private void updateFlinkClusterForRemoteMode(
FlinkCluster paramOfCluster, FlinkCluster flinkCluster) {
flinkCluster.setAddress(paramOfCluster.getAddress());
if (paramOfCluster.getVersionId() != null) {
flinkCluster.setVersionId(paramOfCluster.getVersionId());
}
flinkCluster.setClusterState(ClusterState.RUNNING.getState());
flinkCluster.setStartTime(new Date());
flinkCluster.setEndTime(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class FlinkSqlServiceImpl extends ServiceImpl<FlinkSqlMapper, FlinkSql>
public FlinkSql getEffective(Long appId, boolean decode) {
FlinkSql flinkSql = baseMapper.getEffective(appId);
if (flinkSql != null && decode) {
flinkSql.setSql(DeflaterUtils.unzipString(flinkSql.getSql()));
flinkSql.setSql(DeflaterUtils.toPlainText(flinkSql.getSql()));
}
return flinkSql;
}
Expand All @@ -90,7 +90,7 @@ public FlinkSql getLatestFlinkSql(Long appId, boolean decode) {
.map(
flinkSql -> {
if (decode) {
flinkSql.setSql(DeflaterUtils.unzipString(flinkSql.getSql()));
flinkSql.setSql(DeflaterUtils.toPlainText(flinkSql.getSql()));
}
return flinkSql;
})
Expand All @@ -101,8 +101,7 @@ public FlinkSql getLatestFlinkSql(Long appId, boolean decode) {
public void create(FlinkSql flinkSql) {
Integer version = this.baseMapper.getLatestVersion(flinkSql.getAppId());
flinkSql.setVersion(version == null ? 1 : version + 1);
String sql = DeflaterUtils.zipString(flinkSql.getSql());
flinkSql.setSql(sql);
flinkSql.setSql(DeflaterUtils.compressForStorage(flinkSql.getSql()));
this.save(flinkSql);
this.setCandidate(CandidateTypeEnum.NEW, flinkSql.getAppId(), flinkSql.getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,29 @@ public static void prepareJarJobHome(
boolean tempDirFallback) {
fsOperator.delete(appHome);
if (!fromUpload) {
ApiAlertException.throwIfTrue(
StringUtils.isBlank(distHome),
"[StreamPark] distHome is required for build-resource jar jobs");
fsOperator.upload(distHome, appHome);
return;
}

String uploadJar = appUploads.concat("/").concat(jar);
File localJar = new File(String.format("%s/%d/%s", Workspace.local().APP_UPLOADS(), teamId, jar));
String teamUploads = appUploads.concat("/").concat(String.valueOf(teamId));
String uploadJar = teamUploads.concat("/").concat(jar);
File localJar = new File(uploadJar);
if (!localJar.exists()) {
Resource resource = resourceService.findByResourceName(teamId, jar);
if (resource != null && StringUtils.isNotBlank(resource.getFilePath())) {
localJar = new File(resource.getFilePath());
uploadJar = appUploads.concat("/").concat(localJar.getName());
uploadJar = teamUploads.concat("/").concat(localJar.getName());
} else if (tempDirFallback) {
localJar = new File(WebUtils.getAppTempDir(), jar);
uploadJar = appUploads.concat("/").concat(localJar.getName());
}
}
checkOrElseUploadJar(fsOperator, localJar, uploadJar, appUploads);
if (!localJar.isFile()) {
throw new ApiAlertException("Missing jar file: " + jar + ", please upload again");
}
checkOrElseUploadJar(fsOperator, localJar, uploadJar, teamUploads);

switch (applicationType) {
case STREAMPARK_FLINK:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ void shouldConvertCreateRequestToEntity() {
Assertions.assertEquals(4, app.getDeployMode());
}

@Test
void shouldDefaultNullStartFlagsToFalse() {
FlinkAppStartRequest request = new FlinkAppStartRequest();
request.setId(1L);
request.setTeamId(100000L);

FlinkApplication app = FlinkApplicationAssembler.toEntity(request);

Assertions.assertFalse(app.getRestoreOrTriggerSavepoint());
Assertions.assertFalse(app.getAllowNonRestored());
}

@Test
void shouldConvertStartRequestToEntity() {
FlinkAppStartRequest request = new FlinkAppStartRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public void checkExit(int status) {
public void checkPermission(Permission perm) {
// no-op
}

@Override
public void checkPermission(Permission perm, Object context) {
// no-op
}
}
Loading
Loading