use of com.sohu.cache.entity.InstanceInfo in project cachecloud by sohutv.
the class AppAndInstanceAuthorityInterceptor method preHandle.
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 1. 获取用户
long userId = userLoginStatusService.getUserIdFromLoginStatus(request);
AppUser user = userService.get(userId);
// 2. 管理员直接跳过
if (AppUserTypeEnum.ADMIN_USER.value().equals(user.getType())) {
return true;
}
// 3. 应用id
String appId = request.getParameter("appId");
if (StringUtils.isNotBlank(appId)) {
checkUserAppPower(response, request.getSession(true), user, NumberUtils.toLong(appId));
}
// 4. 实例权限检测(其实也是应用)
String instanceId = request.getParameter("instanceId");
if (StringUtils.isNotBlank(instanceId)) {
InstanceInfo instanceInfo = instanceStatsCenter.getInstanceInfo(Long.parseLong(instanceId));
checkUserAppPower(response, request.getSession(true), user, instanceInfo.getAppId());
}
return true;
}
use of com.sohu.cache.entity.InstanceInfo in project cachecloud by sohutv.
the class RedisDeployCenterImpl method sentinelFailover.
@Override
public boolean sentinelFailover(long appId) throws Exception {
Assert.isTrue(appId > 0);
AppDesc appDesc = appDao.getAppDescById(appId);
Assert.isTrue(appDesc != null);
int type = appDesc.getType();
if (!TypeUtil.isRedisSentinel(type)) {
logger.warn("app={} is not sentinel", appDesc);
return false;
}
final List<InstanceInfo> instanceList = instanceDao.getInstListByAppId(appId);
if (instanceList == null || instanceList.isEmpty()) {
logger.warn("app={} instances is empty");
return false;
}
for (InstanceInfo instanceInfo : instanceList) {
int instanceType = instanceInfo.getType();
if (TypeUtil.isRedisSentinel(instanceType)) {
final String host = instanceInfo.getIp();
final int port = instanceInfo.getPort();
final String masterName = instanceInfo.getCmd();
if (StringUtils.isBlank(masterName)) {
logger.warn("{} cmd is null", instanceInfo);
continue;
}
boolean isRun = redisCenter.isRun(host, port);
if (!isRun) {
logger.warn("{} is not run");
continue;
}
boolean isSentinelFailOver = new IdempotentConfirmer() {
@Override
public boolean execute() {
Jedis jedis = new Jedis(host, port, Protocol.DEFAULT_TIMEOUT);
try {
String response = jedis.sentinelFailover(masterName);
return response != null && response.equalsIgnoreCase("OK");
} finally {
jedis.close();
}
}
}.run();
if (!isSentinelFailOver) {
logger.warn("{}:{} sentienl isSentinelFailOver error", host, port);
return false;
} else {
logger.warn("SentinelFailOver done! ");
break;
}
}
}
return true;
}
use of com.sohu.cache.entity.InstanceInfo in project cachecloud by sohutv.
the class RedisDeployCenterImpl method clusterFailover.
@Override
public boolean clusterFailover(long appId, int slaveInstanceId) throws Exception {
Assert.isTrue(appId > 0);
Assert.isTrue(slaveInstanceId > 0);
AppDesc appDesc = appDao.getAppDescById(appId);
Assert.isTrue(appDesc != null);
int type = appDesc.getType();
if (!TypeUtil.isRedisCluster(type)) {
logger.error("{} is not redis type", appDesc);
return false;
}
InstanceInfo instanceInfo = instanceDao.getInstanceInfoById(slaveInstanceId);
Assert.isTrue(instanceInfo != null);
String slaveHost = instanceInfo.getIp();
int slavePort = instanceInfo.getPort();
final Jedis slaveJedis = new Jedis(slaveHost, slavePort);
boolean isClusterFailOver = new IdempotentConfirmer() {
@Override
public boolean execute() {
String response = slaveJedis.clusterFailoverForce();
return response != null && response.equalsIgnoreCase("OK");
}
}.run();
if (!isClusterFailOver) {
logger.error("{}:{} clusterFailover failed", slaveHost, slavePort);
return false;
} else {
logger.warn("{}:{} clusterFailover Done! ", slaveHost, slavePort);
}
return true;
}
use of com.sohu.cache.entity.InstanceInfo in project cachecloud by sohutv.
the class AppDataMigrateController method appInstanceList.
/**
* 通过应用id获取可用的Redis实例信息
* @return
*/
@RequestMapping(value = "/appInstanceList")
public ModelAndView appInstanceList(HttpServletRequest request, HttpServletResponse response, Model model) {
String appIdStr = request.getParameter("appId");
long appId = NumberUtils.toLong(appIdStr);
AppDesc appDesc = appService.getByAppId(appId);
StringBuffer instances = new StringBuffer();
List<InstanceInfo> instanceList = appService.getAppInstanceInfo(appId);
if (CollectionUtils.isNotEmpty(instanceList)) {
for (int i = 0; i < instanceList.size(); i++) {
InstanceInfo instanceInfo = instanceList.get(i);
if (instanceInfo == null) {
continue;
}
if (instanceInfo.isOffline()) {
continue;
}
// 如果是sentinel类型的应用只出master
if (TypeUtil.isRedisSentinel(appDesc.getType())) {
if (TypeUtil.isRedisSentinel(instanceInfo.getType())) {
continue;
}
if (!redisCenter.isMaster(instanceInfo.getIp(), instanceInfo.getPort())) {
continue;
}
}
instances.append(instanceInfo.getIp() + ":" + instanceInfo.getPort());
if (i != instanceList.size() - 1) {
instances.append(ConstUtils.NEXT_LINE);
}
}
}
model.addAttribute("instances", instances.toString());
model.addAttribute("appType", appDesc == null ? -1 : appDesc.getType());
return new ModelAndView("");
}
use of com.sohu.cache.entity.InstanceInfo in project cachecloud by sohutv.
the class BaseController method fillAppMachineInstanceTopology.
/**
* 应用机器实例分布图
* @param appId
* @param model
*/
protected void fillAppMachineInstanceTopology(Long appId, Model model) {
List<InstanceInfo> instanceList = appService.getAppInstanceInfo(appId);
int groupId = 1;
// 1.分组,同一个主从在一组
for (int i = 0; i < instanceList.size(); i++) {
InstanceInfo instance = instanceList.get(i);
// 有了groupId,不再设置
if (instance.getGroupId() > 0) {
continue;
}
if (instance.isOffline()) {
continue;
}
for (int j = i + 1; j < instanceList.size(); j++) {
InstanceInfo instanceCompare = instanceList.get(j);
if (instanceCompare.isOffline()) {
continue;
}
// 寻找主从对应关系
if (instanceCompare.getMasterInstanceId() == instance.getId() || instance.getMasterInstanceId() == instanceCompare.getId()) {
instanceCompare.setGroupId(groupId);
}
}
instance.setGroupId(groupId++);
}
// 2.机器下的实例列表
Map<String, List<InstanceInfo>> machineInstanceMap = new HashMap<String, List<InstanceInfo>>();
for (InstanceInfo instance : instanceList) {
String ip = instance.getIp();
if (machineInstanceMap.containsKey(ip)) {
machineInstanceMap.get(ip).add(instance);
} else {
List<InstanceInfo> tempInstanceList = new ArrayList<InstanceInfo>();
tempInstanceList.add(instance);
machineInstanceMap.put(ip, tempInstanceList);
}
}
model.addAttribute("machineInstanceMap", machineInstanceMap);
model.addAttribute("instancePairCount", groupId - 1);
}
Aggregations