Skip to content

Commit

Permalink
Merge branch 'azkaban:master' into zhzeng/extract-yarn-methods-to-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tsangz2013 committed Sep 8, 2022
2 parents a35ff0e + 474d470 commit ca8481a
Show file tree
Hide file tree
Showing 20 changed files with 933 additions and 34 deletions.
1 change: 1 addition & 0 deletions az-core/src/main/java/azkaban/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ public static class ImageMgmtConstants {
public static final String VERSION_STATE = "versionState";
public static final String ID_KEY = "id";
public static final String IMAGE_RAMPUP_PLAN = "imageRampupPlan";
public static final String IMAGE_RAMP_RULE = "imageRampRule";
public static final String IMAGE_UPDATE_ADD_USER = "addImageOwners";
public static final String IMAGE_UPDATE_REMOVE_USER = "removeImageOwners";
}
Expand Down
3 changes: 3 additions & 0 deletions azkaban-common/src/main/java/azkaban/AzkabanCommonModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import azkaban.imagemgmt.daos.ImageTypeDaoImpl;
import azkaban.imagemgmt.daos.ImageVersionDao;
import azkaban.imagemgmt.daos.ImageVersionDaoImpl;
import azkaban.imagemgmt.daos.RampRuleDao;
import azkaban.imagemgmt.daos.RampRuleDaoImpl;
import azkaban.imagemgmt.permission.PermissionManager;
import azkaban.imagemgmt.permission.PermissionManagerImpl;
import azkaban.imagemgmt.rampup.ImageRampupManager;
Expand Down Expand Up @@ -224,6 +226,7 @@ private void bindImageManagementDependencies() {
bind(ImageVersionDao.class).to(ImageVersionDaoImpl.class).in(Scopes.SINGLETON);
bind(ImageRampupDao.class).to(ImageRampupDaoImpl.class).in(Scopes.SINGLETON);
bind(ImageRampupManager.class).to(ImageRampupManagerImpl.class).in(Scopes.SINGLETON);
bind(RampRuleDao.class).to(RampRuleDaoImpl.class).in(Scopes.SINGLETON);
bind(ImageMgmtCommonDao.class).to(ImageMgmtCommonDaoImpl.class).in(Scopes.SINGLETON);
bind(PermissionManager.class).to(PermissionManagerImpl.class).in(Scopes.SINGLETON);
bind(Converter.class).annotatedWith(Names.named(IMAGE_TYPE))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2022 LinkedIn Corp.
*
* Licensed 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 azkaban.imagemgmt.daos;

import azkaban.imagemgmt.models.ImageRampRule;


/**
* Data access object (DAO) for accessing image ramp rule that deny an image ramping version for flows.
* This interface defines add/remove/modify/get an image ramp rule.
* */
public interface RampRuleDao {

/**
* Query table ramp_rules to check whether a HPFlowRule .
*
* @param ruleName - ruleName in {@see ImageRampRule}
* @return true - if ramp rule is setup for HP Flows, denying any ramp up plan;
* false - if ramp rule is normal rule, regulate only on certain image version.
*/
boolean isHPFlowRule(final String ruleName);

/**
* Insert Image Ramp Rule metadata into DB.
*
* @param rule - RampRule model created in {@see ImageRampRuleServiceImpl}
* @return int - id of the DB entry
* @throws azkaban.imagemgmt.exception.ImageMgmtDaoException
*/
int addRampRule(final ImageRampRule rule);

/**
* Query table ramp_rules to get owners of Ramp rule.
*
* @param ruleName - ruleName in {@see ImageRampRule}
* @return owners of the ramp rule.
*/
String getOwners(final String ruleName);

/**
* delete the ramp_rules to get owners of Ramp rule.
*
* @param ruleName - ruleName in {@see ImageRampRule}
* @throws azkaban.imagemgmt.exception.ImageMgmtDaoException
*/
void deleteRampRule(final String ruleName);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright 2022 LinkedIn Corp.
*
* Licensed 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 azkaban.imagemgmt.daos;

import azkaban.db.DatabaseOperator;
import azkaban.imagemgmt.exception.ErrorCode;
import azkaban.imagemgmt.exception.ImageMgmtDaoException;
import azkaban.imagemgmt.models.ImageRampRule;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.log4j.Logger;
import org.bouncycastle.util.Strings;

/**
* Dao Implementation for access DB of table ramp_rules.
* Create/update/delete/get RampRule or partial RampRule metadata. {@link ImageRampRule}
* */
@Singleton
public class RampRuleDaoImpl implements RampRuleDao {
private static final Logger LOG = Logger.getLogger(RampRuleDaoImpl.class);
private final DatabaseOperator databaseOperator;

private static String INSERT_RAMP_RULE = "INSERT into ramp_rules "
+ "(rule_name, image_name, image_version, owners, is_HP, created_by, created_on, modified_by, modified_on)"
+ " values (?, ?, ?, ?, ?, ?, ?, ?, ?)";

@Inject
public RampRuleDaoImpl(final DatabaseOperator databaseOperator) {
this.databaseOperator = databaseOperator;
}

@Override
public boolean isHPFlowRule(final String ruleName) {
return false;
}

/**
* Insert new ramp rule into DB, check if duplicate ruleName exists first
*
* @param rampRule
* @throws ImageMgmtDaoException
* */
@Override
public int addRampRule(final ImageRampRule rampRule) {
try {
// duplicated rule should be forbidden as client side error, which needs a separate check to
// avoid it falls into normal SQL Exception as server error.
List<ImageRampRule> rampRules = databaseOperator.query(
FetchRampRuleHandler.FETCH_RAMP_RULE_BY_ID, new FetchRampRuleHandler(), rampRule.getRuleName());
if (!rampRules.isEmpty()) {
LOG.error("Error in create ramp rule on duplicate ruleName: " + rampRule.getRuleName());
throw new ImageMgmtDaoException(ErrorCode.BAD_REQUEST,
"Error in create ramp rule on duplicate ruleName: " + rampRule.getRuleName());
}
return this.databaseOperator.update(INSERT_RAMP_RULE,
rampRule.getRuleName(),
rampRule.getImageName(),
rampRule.getImageVersion(),
rampRule.getOwners(),
rampRule.isHPRule(),
rampRule.getCreatedBy(),
Timestamp.valueOf(LocalDateTime.now()),
rampRule.getCreatedBy(),
Timestamp.valueOf(LocalDateTime.now()));
} catch (SQLException e) {
LOG.error("Error in create ramp rule on DB" + rampRule);
throw new ImageMgmtDaoException(ErrorCode.INTERNAL_SERVER_ERROR,
"Error creating ramp rule " + rampRule.getRuleName() + "with " + e.getMessage());
}
}

@Override
public String getOwners(final String ruleName) {
return null;
}

@Override
public void deleteRampRule(final String ruleName) {

}

public static class FetchRampRuleHandler implements ResultSetHandler<List<ImageRampRule>> {

private static final String FETCH_RAMP_RULE_BY_ID =
"SELECT rule_name, image_name, image_version, owners, is_HP"
+ " FROM ramp_rules WHERE rule_name = ?";

@Override
public List<ImageRampRule> handle(final ResultSet rs) throws SQLException {
if (!rs.next()) {
return Collections.emptyList();
}
final List<ImageRampRule> imageRampRules = new ArrayList<>();
do {
final String ruleName = rs.getString("rule_name");
final String imageName = rs.getString("image_name");
final String imageVersion = rs.getString("image_version");
final String owners = rs.getString("owners");
final boolean isHP = rs.getBoolean("is_HP");
Set<String> ownerLists = new HashSet<>(Arrays.asList(Strings.split(owners, ',')));
final ImageRampRule imageRampRule = new ImageRampRule.Builder()
.setRuleName(ruleName)
.setImageName(imageName)
.setImageVersion(imageVersion)
.setOwners(ownerLists)
.setHPRule(isHP)
.build();
imageRampRules.add(imageRampRule);
} while (rs.next());
return imageRampRules;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2022 LinkedIn Corp.
*
* Licensed 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 azkaban.imagemgmt.dto;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import org.codehaus.jackson.annotate.JsonProperty;

/**
* This class represents ImageRampRule Request when receiving request body from user.
* */
public class ImageRampRuleRequestDTO extends BaseDTO {
// Represents the name of the Ramp rule
@JsonProperty("ruleName")
@NotBlank(message = "ruleName cannot be blank.", groups = ValidationOnCreate.class)
@NotNull(message = "ruleName cannot be null.")
private String ruleName;
// Represents the name of the image type for the rule
@JsonProperty("imageName")
@NotBlank(message = "imageName cannot be blank.", groups = {ValidationOnCreate.class})
@NotNull(message = "imageName cannot be null.")
private String imageName;
// Represents the name of the image version for the rule
@JsonProperty("imageVersion")
@NotBlank(message = "imageVersion cannot be blank.", groups = {ValidationOnCreate.class})
@NotNull(message = "imageVersion cannot be null.")
private String imageVersion;

public void setRuleName(String ruleName) {
this.ruleName = ruleName;
}

public void setImageName(String imageName) {
this.imageName = imageName;
}

public void setImageVersion(String imageVersion) {
this.imageVersion = imageVersion;
}

public String getRuleName() {
return ruleName;
}

public String getImageName() {
return imageName;
}

public String getImageVersion() {
return imageVersion;
}

}
Loading

0 comments on commit ca8481a

Please sign in to comment.