forked from apache/fineract
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FINERACT-2194: Batch jobs arre failing in a multitenant environment
- Loading branch information
Showing
8 changed files
with
223 additions
and
76 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...rc/main/java/org/apache/fineract/infrastructure/jobs/TenantAwareEqualsHashCodeAdvice.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,56 @@ | ||
/** | ||
* 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.fineract.infrastructure.jobs; | ||
|
||
import java.util.Objects; | ||
import org.aopalliance.intercept.MethodInterceptor; | ||
import org.aopalliance.intercept.MethodInvocation; | ||
import org.apache.fineract.infrastructure.core.domain.FineractPlatformTenant; | ||
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class TenantAwareEqualsHashCodeAdvice implements MethodInterceptor { | ||
|
||
private final Object target; | ||
private final String tenantIdentifier; | ||
|
||
public TenantAwareEqualsHashCodeAdvice(Object target) { | ||
this.target = target; | ||
FineractPlatformTenant tenant = ThreadLocalContextUtil.getTenant(); | ||
this.tenantIdentifier = tenant != null ? tenant.getTenantIdentifier() : null; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Object invoke(@NotNull MethodInvocation invocation) throws Throwable { | ||
String methodName = invocation.getMethod().getName(); | ||
|
||
if (methodName.equals("equals") && invocation.getArguments().length == 1) { | ||
Object other = invocation.getArguments()[0]; | ||
return Objects.equals(tenantIdentifier, ((TenantAwareEqualsHashCodeAdvice) target).tenantIdentifier) && target.equals(other); | ||
} | ||
|
||
if (methodName.equals("hashCode") && invocation.getArguments().length == 0) { | ||
return Objects.hash(target.hashCode(), tenantIdentifier); | ||
} | ||
|
||
return invocation.proceed(); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...rc/main/java/org/springframework/batch/core/scope/context/StepSynchronizationManager.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,65 @@ | ||
/** | ||
* 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.springframework.batch.core.scope.context; | ||
|
||
import org.apache.fineract.infrastructure.jobs.TenantAwareEqualsHashCodeAdvice; | ||
import org.springframework.aop.framework.ProxyFactory; | ||
import org.springframework.batch.core.StepExecution; | ||
import org.springframework.lang.Nullable; | ||
|
||
// Temporary solution until spring-batch fixes the concurrency issue | ||
// https://github.com/spring-projects/spring-batch/issues/4774 | ||
// Mostly copy from spring-batch | ||
@SuppressWarnings({ "HideUtilityClassConstructor" }) | ||
public class StepSynchronizationManager { | ||
|
||
private static final SynchronizationManagerSupport<StepExecution, StepContext> manager = new SynchronizationManagerSupport<>() { | ||
|
||
@Override | ||
protected StepContext createNewContext(StepExecution execution) { | ||
return new StepContext(execution); | ||
} | ||
|
||
@Override | ||
protected void close(StepContext context) { | ||
context.close(); | ||
} | ||
}; | ||
|
||
@Nullable | ||
public static StepContext getContext() { | ||
return manager.getContext(); | ||
} | ||
|
||
public static StepContext register(StepExecution stepExecution) { | ||
ProxyFactory factory = new ProxyFactory(stepExecution); | ||
factory.setProxyTargetClass(true); | ||
factory.addAdvice(new TenantAwareEqualsHashCodeAdvice(stepExecution)); | ||
return manager.register((StepExecution) factory.getProxy()); | ||
} | ||
|
||
public static void close() { | ||
manager.close(); | ||
} | ||
|
||
public static void release() { | ||
manager.release(); | ||
} | ||
|
||
} |
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
Oops, something went wrong.