Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encapsulate the logic to route between RO and RW DB instance. That way, we always honor the perThreadDirtyDBFlag. Signed-off-by: Pierre-Alexandre Meyer <pierre@mouraf.org>
- Loading branch information
Showing
13 changed files
with
212 additions
and
118 deletions.
There are no files selected for viewing
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
68 changes: 35 additions & 33 deletions
68
util/src/main/java/org/killbill/billing/util/audit/dao/DefaultAuditDao.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
40 changes: 40 additions & 0 deletions
40
util/src/main/java/org/killbill/billing/util/entity/dao/DBRouter.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,40 @@ | ||
/* | ||
* Copyright 2014-2018 Groupon, Inc | ||
* Copyright 2014-2018 The Billing Project, LLC | ||
* | ||
* The Billing Project 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.killbill.billing.util.entity.dao; | ||
|
||
import org.skife.jdbi.v2.IDBI; | ||
|
||
public class DBRouter<C> extends DBRouterUntyped { | ||
|
||
private final C onDemand; | ||
private final C roOnDemand; | ||
|
||
public DBRouter(final IDBI dbi, final IDBI roDbi, final Class<C> sqlObjectType) { | ||
super(dbi, roDbi); | ||
this.onDemand = dbi.onDemand(sqlObjectType); | ||
this.roOnDemand = roDbi.onDemand(sqlObjectType); | ||
} | ||
|
||
public C onDemand(final boolean requestedRO) { | ||
if (shouldUseRODBI(requestedRO)) { | ||
return roOnDemand; | ||
} else { | ||
return onDemand; | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
util/src/main/java/org/killbill/billing/util/entity/dao/DBRouterUntyped.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,79 @@ | ||
/* | ||
* Copyright 2014-2018 Groupon, Inc | ||
* Copyright 2014-2018 The Billing Project, LLC | ||
* | ||
* The Billing Project 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.killbill.billing.util.entity.dao; | ||
|
||
import org.killbill.billing.util.glue.KillbillApiAopModule; | ||
import org.skife.jdbi.v2.Handle; | ||
import org.skife.jdbi.v2.IDBI; | ||
import org.skife.jdbi.v2.TransactionCallback; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class DBRouterUntyped { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(DBRouterUntyped.class); | ||
|
||
protected final IDBI dbi; | ||
protected final IDBI roDbi; | ||
|
||
public DBRouterUntyped(final IDBI dbi, final IDBI roDbi) { | ||
this.dbi = dbi; | ||
this.roDbi = roDbi; | ||
} | ||
|
||
public Handle getHandle(final boolean requestedRO) { | ||
if (shouldUseRODBI(requestedRO)) { | ||
return roDbi.open(); | ||
} else { | ||
return dbi.open(); | ||
} | ||
} | ||
|
||
public <T> T onDemand(final boolean requestedRO, final Class<T> sqlObjectType) { | ||
if (shouldUseRODBI(requestedRO)) { | ||
return roDbi.onDemand(sqlObjectType); | ||
} else { | ||
return dbi.onDemand(sqlObjectType); | ||
} | ||
} | ||
|
||
public <T> T inTransaction(final boolean requestedRO, final TransactionCallback<T> callback) { | ||
if (shouldUseRODBI(requestedRO)) { | ||
return roDbi.inTransaction(callback); | ||
} else { | ||
return dbi.inTransaction(callback); | ||
} | ||
} | ||
|
||
boolean shouldUseRODBI(final boolean requestedRO) { | ||
if (!requestedRO) { | ||
KillbillApiAopModule.setDirtyDBFlag(); | ||
logger.debug("Dirty flag set, using RW DBI"); | ||
return false; | ||
} else { | ||
if (KillbillApiAopModule.getDirtyDBFlag()) { | ||
// Redirect to the rw instance, to work-around any replication delay | ||
logger.debug("RO DBI handle requested, but dirty flag set, using RW DBI"); | ||
return false; | ||
} else { | ||
logger.debug("Using RO DBI"); | ||
return true; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.