Browse Source
* add cache manager for workflow * [DS-6988][MasterServer] add cache manager for workflow * cache evict code optimization * test Co-authored-by: caishunfeng <534328519@qq.com>3.0.0/version-upgrade
wind
3 years ago
committed by
GitHub
37 changed files with 543 additions and 773 deletions
@ -0,0 +1,102 @@ |
|||||||
|
/* |
||||||
|
* 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.api.aspect; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.common.enums.CacheType; |
||||||
|
import org.apache.dolphinscheduler.remote.command.CacheExpireCommand; |
||||||
|
import org.apache.dolphinscheduler.service.cache.CacheNotifyService; |
||||||
|
import org.apache.dolphinscheduler.service.cache.impl.CacheKeyGenerator; |
||||||
|
|
||||||
|
import java.lang.reflect.Method; |
||||||
|
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint; |
||||||
|
import org.aspectj.lang.annotation.Around; |
||||||
|
import org.aspectj.lang.annotation.Aspect; |
||||||
|
import org.aspectj.lang.annotation.Pointcut; |
||||||
|
import org.aspectj.lang.reflect.MethodSignature; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.cache.annotation.CacheConfig; |
||||||
|
import org.springframework.cache.annotation.CacheEvict; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
/** |
||||||
|
* aspect for cache evict |
||||||
|
*/ |
||||||
|
@Aspect |
||||||
|
@Component |
||||||
|
public class CacheEvictAspect { |
||||||
|
|
||||||
|
private static final String UPDATE_BY_ID = "updateById"; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private CacheKeyGenerator cacheKeyGenerator; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private CacheNotifyService cacheNotifyService; |
||||||
|
|
||||||
|
@Pointcut("@annotation(org.springframework.cache.annotation.CacheEvict)") |
||||||
|
public void cacheEvictPointCut() { |
||||||
|
// Do nothing because of it's a pointcut
|
||||||
|
} |
||||||
|
|
||||||
|
@Around("cacheEvictPointCut()") |
||||||
|
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { |
||||||
|
MethodSignature sign = (MethodSignature) proceedingJoinPoint.getSignature(); |
||||||
|
Method method = sign.getMethod(); |
||||||
|
Object target = proceedingJoinPoint.getTarget(); |
||||||
|
Object[] args = proceedingJoinPoint.getArgs(); |
||||||
|
|
||||||
|
Object result = proceedingJoinPoint.proceed(); |
||||||
|
|
||||||
|
CacheConfig cacheConfig = method.getDeclaringClass().getAnnotation(CacheConfig.class); |
||||||
|
CacheEvict cacheEvict = method.getAnnotation(CacheEvict.class); |
||||||
|
|
||||||
|
CacheType cacheType = getCacheType(cacheConfig, cacheEvict); |
||||||
|
if (cacheType != null) { |
||||||
|
// todo use springEL is better
|
||||||
|
if (method.getName().equalsIgnoreCase(UPDATE_BY_ID) && args.length == 1) { |
||||||
|
Object updateObj = args[0]; |
||||||
|
cacheNotifyService.notifyMaster(new CacheExpireCommand(cacheType, updateObj).convert2Command()); |
||||||
|
} else { |
||||||
|
Object key = cacheKeyGenerator.generate(target, method, args); |
||||||
|
cacheNotifyService.notifyMaster(new CacheExpireCommand(cacheType, key).convert2Command()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
private CacheType getCacheType(CacheConfig cacheConfig, CacheEvict cacheEvict) { |
||||||
|
String cacheName = null; |
||||||
|
if (cacheEvict.cacheNames().length > 0) { |
||||||
|
cacheName = cacheEvict.cacheNames()[0]; |
||||||
|
} |
||||||
|
if (cacheConfig.cacheNames().length > 0) { |
||||||
|
cacheName = cacheConfig.cacheNames()[0]; |
||||||
|
} |
||||||
|
if (cacheName == null) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
for (CacheType cacheType : CacheType.values()) { |
||||||
|
if (cacheType.getCacheName().equals(cacheName)) { |
||||||
|
return cacheType; |
||||||
|
} |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
@ -1,22 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.service.cache.processor; |
|
||||||
|
|
||||||
public interface BaseCacheProcessor { |
|
||||||
void cacheExpire(Class updateObjClass, String updateObjJson); |
|
||||||
} |
|
@ -1,22 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.service.cache.processor; |
|
||||||
|
|
||||||
public interface QueueCacheProcessor extends BaseCacheProcessor { |
|
||||||
public void expireAllUserCache(); |
|
||||||
} |
|
@ -1,26 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.service.cache.processor; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.dao.entity.Tenant; |
|
||||||
|
|
||||||
public interface TenantCacheProcessor extends BaseCacheProcessor { |
|
||||||
void update(int tenantId); |
|
||||||
|
|
||||||
Tenant queryById(int tenantId); |
|
||||||
} |
|
@ -1,58 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.service.cache.processor.impl; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.common.enums.CacheType; |
|
||||||
import org.apache.dolphinscheduler.service.cache.processor.BaseCacheProcessor; |
|
||||||
import org.apache.dolphinscheduler.service.cache.processor.QueueCacheProcessor; |
|
||||||
import org.apache.dolphinscheduler.service.cache.processor.TenantCacheProcessor; |
|
||||||
import org.apache.dolphinscheduler.service.cache.processor.UserCacheProcessor; |
|
||||||
|
|
||||||
import java.util.Map; |
|
||||||
import java.util.concurrent.ConcurrentHashMap; |
|
||||||
|
|
||||||
import javax.annotation.PostConstruct; |
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||||
import org.springframework.stereotype.Component; |
|
||||||
|
|
||||||
@Component |
|
||||||
public class CacheProcessorFactory { |
|
||||||
|
|
||||||
@Autowired |
|
||||||
private TenantCacheProcessor tenantCacheProcessor; |
|
||||||
|
|
||||||
@Autowired |
|
||||||
private UserCacheProcessor userCacheProcessor; |
|
||||||
|
|
||||||
@Autowired |
|
||||||
private QueueCacheProcessor queueCacheProcessor; |
|
||||||
|
|
||||||
Map<CacheType, BaseCacheProcessor> cacheProcessorMap = new ConcurrentHashMap<>(); |
|
||||||
|
|
||||||
@PostConstruct |
|
||||||
private void init() { |
|
||||||
cacheProcessorMap.put(CacheType.TENANT, tenantCacheProcessor); |
|
||||||
cacheProcessorMap.put(CacheType.USER, userCacheProcessor); |
|
||||||
cacheProcessorMap.put(CacheType.QUEUE, queueCacheProcessor); |
|
||||||
} |
|
||||||
|
|
||||||
public BaseCacheProcessor getCacheProcessor(CacheType cacheType) { |
|
||||||
return cacheProcessorMap.get(cacheType); |
|
||||||
} |
|
||||||
} |
|
@ -1,50 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.service.cache.processor.impl; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.common.utils.JSONUtils; |
|
||||||
import org.apache.dolphinscheduler.dao.entity.Queue; |
|
||||||
import org.apache.dolphinscheduler.service.bean.SpringApplicationContext; |
|
||||||
import org.apache.dolphinscheduler.service.cache.processor.QueueCacheProcessor; |
|
||||||
|
|
||||||
import org.slf4j.Logger; |
|
||||||
import org.slf4j.LoggerFactory; |
|
||||||
import org.springframework.cache.annotation.CacheEvict; |
|
||||||
import org.springframework.stereotype.Component; |
|
||||||
|
|
||||||
@Component |
|
||||||
public class QueueCacheProcessorImpl implements QueueCacheProcessor { |
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(getClass()); |
|
||||||
|
|
||||||
@Override |
|
||||||
@CacheEvict(cacheNames = "user", allEntries = true) |
|
||||||
public void expireAllUserCache() { |
|
||||||
// just evict cache
|
|
||||||
logger.debug("expire all user cache"); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void cacheExpire(Class updateObjClass, String updateObjJson) { |
|
||||||
Queue updateQueue = (Queue) JSONUtils.parseObject(updateObjJson, updateObjClass); |
|
||||||
if (updateQueue == null) { |
|
||||||
return; |
|
||||||
} |
|
||||||
SpringApplicationContext.getBean(QueueCacheProcessor.class).expireAllUserCache(); |
|
||||||
} |
|
||||||
} |
|
@ -1,64 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.service.cache.processor.impl; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.common.utils.JSONUtils; |
|
||||||
import org.apache.dolphinscheduler.dao.entity.Tenant; |
|
||||||
import org.apache.dolphinscheduler.dao.mapper.TenantMapper; |
|
||||||
import org.apache.dolphinscheduler.service.bean.SpringApplicationContext; |
|
||||||
import org.apache.dolphinscheduler.service.cache.processor.TenantCacheProcessor; |
|
||||||
|
|
||||||
import org.slf4j.Logger; |
|
||||||
import org.slf4j.LoggerFactory; |
|
||||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||||
import org.springframework.cache.annotation.CacheConfig; |
|
||||||
import org.springframework.cache.annotation.CacheEvict; |
|
||||||
import org.springframework.cache.annotation.Cacheable; |
|
||||||
import org.springframework.stereotype.Component; |
|
||||||
|
|
||||||
@Component |
|
||||||
@CacheConfig(cacheNames = "tenant") |
|
||||||
public class TenantCacheProcessorImpl implements TenantCacheProcessor { |
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(getClass()); |
|
||||||
|
|
||||||
@Autowired |
|
||||||
private TenantMapper tenantMapper; |
|
||||||
|
|
||||||
@Override |
|
||||||
@CacheEvict |
|
||||||
public void update(int tenantId) { |
|
||||||
// just evict cache
|
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
@Cacheable(sync = true) |
|
||||||
public Tenant queryById(int tenantId) { |
|
||||||
logger.debug("tenant cache proxy, tenantId:{}", tenantId); |
|
||||||
return tenantMapper.queryById(tenantId); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void cacheExpire(Class updateObjClass, String updateObjJson) { |
|
||||||
Tenant updateTenant = (Tenant) JSONUtils.parseObject(updateObjJson, updateObjClass); |
|
||||||
if (updateTenant == null) { |
|
||||||
return; |
|
||||||
} |
|
||||||
SpringApplicationContext.getBean(TenantCacheProcessor.class).update(updateTenant.getId()); |
|
||||||
} |
|
||||||
} |
|
@ -1,59 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.service.cache.processor.impl; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.common.utils.JSONUtils; |
|
||||||
import org.apache.dolphinscheduler.dao.entity.User; |
|
||||||
import org.apache.dolphinscheduler.dao.mapper.UserMapper; |
|
||||||
import org.apache.dolphinscheduler.service.bean.SpringApplicationContext; |
|
||||||
import org.apache.dolphinscheduler.service.cache.processor.UserCacheProcessor; |
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||||
import org.springframework.cache.annotation.CacheConfig; |
|
||||||
import org.springframework.cache.annotation.CacheEvict; |
|
||||||
import org.springframework.cache.annotation.Cacheable; |
|
||||||
import org.springframework.stereotype.Component; |
|
||||||
|
|
||||||
@Component |
|
||||||
@CacheConfig(cacheNames = "user") |
|
||||||
public class UserCacheProcessorImpl implements UserCacheProcessor { |
|
||||||
|
|
||||||
@Autowired |
|
||||||
private UserMapper userMapper; |
|
||||||
|
|
||||||
@Override |
|
||||||
@CacheEvict |
|
||||||
public void update(int userId) { |
|
||||||
// just evict cache
|
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
@Cacheable(sync = true) |
|
||||||
public User selectById(int userId) { |
|
||||||
return userMapper.selectById(userId); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void cacheExpire(Class updateObjClass, String updateObjJson) { |
|
||||||
User user = (User) JSONUtils.parseObject(updateObjJson, updateObjClass); |
|
||||||
if (user == null) { |
|
||||||
return; |
|
||||||
} |
|
||||||
SpringApplicationContext.getBean(UserCacheProcessor.class).update(user.getId()); |
|
||||||
} |
|
||||||
} |
|
@ -1,60 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.service.cache.processor; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.common.utils.JSONUtils; |
|
||||||
import org.apache.dolphinscheduler.dao.entity.Queue; |
|
||||||
import org.apache.dolphinscheduler.service.bean.SpringApplicationContext; |
|
||||||
import org.apache.dolphinscheduler.service.cache.processor.impl.QueueCacheProcessorImpl; |
|
||||||
|
|
||||||
import org.junit.Before; |
|
||||||
import org.junit.Rule; |
|
||||||
import org.junit.Test; |
|
||||||
import org.junit.rules.ExpectedException; |
|
||||||
import org.junit.runner.RunWith; |
|
||||||
import org.mockito.InjectMocks; |
|
||||||
import org.powermock.api.mockito.PowerMockito; |
|
||||||
import org.powermock.core.classloader.annotations.PrepareForTest; |
|
||||||
import org.powermock.modules.junit4.PowerMockRunner; |
|
||||||
|
|
||||||
/** |
|
||||||
* tenant cache proxy test |
|
||||||
*/ |
|
||||||
@RunWith(PowerMockRunner.class) |
|
||||||
@PrepareForTest({SpringApplicationContext.class}) |
|
||||||
public class QueueCacheProcessorTest { |
|
||||||
|
|
||||||
@Rule |
|
||||||
public final ExpectedException exception = ExpectedException.none(); |
|
||||||
|
|
||||||
@InjectMocks |
|
||||||
private QueueCacheProcessorImpl queueCacheProcessor; |
|
||||||
|
|
||||||
@Before |
|
||||||
public void before() { |
|
||||||
PowerMockito.mockStatic(SpringApplicationContext.class); |
|
||||||
PowerMockito.when(SpringApplicationContext.getBean(QueueCacheProcessor.class)).thenReturn(queueCacheProcessor); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testCacheExpire() { |
|
||||||
Queue queue = new Queue(); |
|
||||||
queue.setId(100); |
|
||||||
queueCacheProcessor.cacheExpire(Queue.class, JSONUtils.toJsonString(queue)); |
|
||||||
} |
|
||||||
} |
|
@ -1,78 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.service.cache.processor; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.common.utils.JSONUtils; |
|
||||||
import org.apache.dolphinscheduler.dao.entity.Tenant; |
|
||||||
import org.apache.dolphinscheduler.dao.mapper.TenantMapper; |
|
||||||
import org.apache.dolphinscheduler.service.bean.SpringApplicationContext; |
|
||||||
import org.apache.dolphinscheduler.service.cache.processor.impl.TenantCacheProcessorImpl; |
|
||||||
|
|
||||||
import org.junit.Assert; |
|
||||||
import org.junit.Before; |
|
||||||
import org.junit.Rule; |
|
||||||
import org.junit.Test; |
|
||||||
import org.junit.rules.ExpectedException; |
|
||||||
import org.junit.runner.RunWith; |
|
||||||
import org.mockito.InjectMocks; |
|
||||||
import org.mockito.Mock; |
|
||||||
import org.mockito.Mockito; |
|
||||||
import org.powermock.api.mockito.PowerMockito; |
|
||||||
import org.powermock.core.classloader.annotations.PrepareForTest; |
|
||||||
import org.powermock.modules.junit4.PowerMockRunner; |
|
||||||
|
|
||||||
/** |
|
||||||
* tenant cache proxy test |
|
||||||
*/ |
|
||||||
@RunWith(PowerMockRunner.class) |
|
||||||
@PrepareForTest({SpringApplicationContext.class}) |
|
||||||
public class TenantCacheProcessorTest { |
|
||||||
|
|
||||||
@Rule |
|
||||||
public final ExpectedException exception = ExpectedException.none(); |
|
||||||
|
|
||||||
@InjectMocks |
|
||||||
private TenantCacheProcessorImpl tenantCacheProcessor; |
|
||||||
|
|
||||||
@Mock |
|
||||||
private TenantMapper tenantMapper; |
|
||||||
|
|
||||||
@Before |
|
||||||
public void before() { |
|
||||||
PowerMockito.mockStatic(SpringApplicationContext.class); |
|
||||||
PowerMockito.when(SpringApplicationContext.getBean(TenantCacheProcessor.class)).thenReturn(tenantCacheProcessor); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testQueryById() { |
|
||||||
Tenant tenant1 = new Tenant(); |
|
||||||
tenant1.setId(100); |
|
||||||
tenant1.setDescription("test1"); |
|
||||||
|
|
||||||
Mockito.when(tenantMapper.queryById(100)).thenReturn(tenant1); |
|
||||||
Assert.assertEquals(tenant1, tenantCacheProcessor.queryById(100)); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testCacheExpire() { |
|
||||||
Tenant tenant1 = new Tenant(); |
|
||||||
tenant1.setId(100); |
|
||||||
tenant1.setDescription("test1"); |
|
||||||
tenantCacheProcessor.cacheExpire(Tenant.class, JSONUtils.toJsonString(tenant1)); |
|
||||||
} |
|
||||||
} |
|
@ -1,76 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.service.cache.processor; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.common.utils.JSONUtils; |
|
||||||
import org.apache.dolphinscheduler.dao.entity.User; |
|
||||||
import org.apache.dolphinscheduler.dao.mapper.UserMapper; |
|
||||||
import org.apache.dolphinscheduler.service.bean.SpringApplicationContext; |
|
||||||
import org.apache.dolphinscheduler.service.cache.processor.impl.UserCacheProcessorImpl; |
|
||||||
|
|
||||||
import org.junit.Assert; |
|
||||||
import org.junit.Before; |
|
||||||
import org.junit.Rule; |
|
||||||
import org.junit.Test; |
|
||||||
import org.junit.rules.ExpectedException; |
|
||||||
import org.junit.runner.RunWith; |
|
||||||
import org.mockito.InjectMocks; |
|
||||||
import org.mockito.Mock; |
|
||||||
import org.mockito.Mockito; |
|
||||||
import org.powermock.api.mockito.PowerMockito; |
|
||||||
import org.powermock.core.classloader.annotations.PrepareForTest; |
|
||||||
import org.powermock.modules.junit4.PowerMockRunner; |
|
||||||
|
|
||||||
/** |
|
||||||
* tenant cache proxy test |
|
||||||
*/ |
|
||||||
@RunWith(PowerMockRunner.class) |
|
||||||
@PrepareForTest({SpringApplicationContext.class}) |
|
||||||
public class UserCacheProxyTest { |
|
||||||
|
|
||||||
@Rule |
|
||||||
public final ExpectedException exception = ExpectedException.none(); |
|
||||||
|
|
||||||
@InjectMocks |
|
||||||
private UserCacheProcessorImpl userCacheProcessor; |
|
||||||
|
|
||||||
@Mock |
|
||||||
private UserMapper userMapper; |
|
||||||
|
|
||||||
@Before |
|
||||||
public void before() { |
|
||||||
PowerMockito.mockStatic(SpringApplicationContext.class); |
|
||||||
PowerMockito.when(SpringApplicationContext.getBean(UserCacheProcessor.class)).thenReturn(userCacheProcessor); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testQueryById() { |
|
||||||
User user1 = new User(); |
|
||||||
user1.setId(100); |
|
||||||
|
|
||||||
Mockito.when(userMapper.selectById(100)).thenReturn(user1); |
|
||||||
Assert.assertEquals(user1, userCacheProcessor.selectById(100)); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testCacheExpire() { |
|
||||||
User user = new User(); |
|
||||||
user.setId(100); |
|
||||||
userCacheProcessor.cacheExpire(User.class, JSONUtils.toJsonString(user)); |
|
||||||
} |
|
||||||
} |
|
Loading…
Reference in new issue