Skip to content

Commit

Permalink
fix(ZNTA-2730): clear hibernate cache after batch
Browse files Browse the repository at this point in the history
  • Loading branch information
djansen-redhat committed Aug 7, 2018
1 parent 3efc4e1 commit 191b3a1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Expand Up @@ -228,7 +228,6 @@ public Future<Void> prefillProjectVersionWithMachineTranslation(
for (Iterator<HDocument> iterator = documents.values().iterator();
iterator.hasNext() && !(cancelled = taskHandle.isCancelled()); ) {
HDocument doc = iterator.next();
Long docId = doc.getId();

if (!attributionService.supportsAttribution(doc)) {
log.warn("Attribution not supported for {}; skipping MT", doc);
Expand Down
Expand Up @@ -21,7 +21,11 @@
package org.zanata.service.impl;

import static org.zanata.transaction.TransactionUtilImpl.runInTransaction;

import java.lang.reflect.Field;
import java.text.MessageFormat;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
Expand All @@ -40,6 +44,9 @@
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.deltaspike.jpa.api.transaction.Transactional;
import org.hibernate.engine.internal.StatefulPersistenceContext;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.SessionImplementor;
import org.jetbrains.annotations.NotNull;
import org.zanata.async.Async;
import org.zanata.async.AsyncTaskHandle;
Expand Down Expand Up @@ -178,10 +185,26 @@ private List<TranslationResult> translate(LocaleId localeId,
projectIteration, hLocale);
List<TextFlowTargetStateChange> targetStates = Lists.newArrayList();
Map<ContentState, Long> contentStateDeltas = Maps.newHashMap();

System.out.println(" Processing translations...");
Instant start = Instant.now();
try {
SessionImplementor sessionImpl = (SessionImplementor) entityManager.getDelegate();
PersistenceContext persistenceContext = sessionImpl.getPersistenceContext();
Field entityEntriesField = StatefulPersistenceContext.class.getDeclaredField("entitiesByKey");
entityEntriesField.setAccessible(true);
Map map = (Map) entityEntriesField.get(persistenceContext);
log.info("Pre-entitiesManager size {}", map.size());
} catch (Exception e)
{
log.error(e.getMessage());
}

for (TransUnitUpdateRequest request : translationRequests) {
ContentState newContentState = request.getNewContentState();
HTextFlow hTextFlow = entityManager.find(HTextFlow.class,
request.getTransUnitId().getValue());

TranslationResultImpl result = new TranslationResultImpl();
result.similarityPercent = request.getSimilarityPercent();
if (runValidation) {
Expand Down Expand Up @@ -257,6 +280,18 @@ private List<TranslationResult> translate(LocaleId localeId,
result.translatedTextFlowTarget = hTextFlowTarget;
results.add(result);
}
try {
SessionImplementor sessionImpl = (SessionImplementor) entityManager.getDelegate();
PersistenceContext persistenceContext = sessionImpl.getPersistenceContext();
Field entityEntriesField = StatefulPersistenceContext.class.getDeclaredField("entitiesByKey");
entityEntriesField.setAccessible(true);
Map map = (Map) entityEntriesField.get(persistenceContext);
log.info("Post EntityManager size {}", map.size());
} catch (Exception e)
{
log.error(e.getMessage());
}
System.out.println(" Done processing (" + Duration.between(start, Instant.now()).toMillis() + ")");
if (!targetStates.isEmpty()) {
DocumentLocaleKey documentLocaleKey =
new DocumentLocaleKey(sampleHTextFlow.getDocument().getId(),
Expand Down

0 comments on commit 191b3a1

Please sign in to comment.