More fix build

This commit is contained in:
James Agnew 2015-08-31 09:40:44 -04:00
parent fc36dabc68
commit 71c28b5709
1 changed files with 70 additions and 64 deletions

View File

@ -53,6 +53,7 @@ import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.rest.api.RestOperationTypeEnum; import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
import ca.uhn.fhir.rest.server.IBundleProvider; import ca.uhn.fhir.rest.server.IBundleProvider;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor.ActionRequestDetails; import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor.ActionRequestDetails;
public abstract class BaseHapiFhirSystemDao<T> extends BaseHapiFhirDao<IBaseResource>implements IFhirSystemDao<T> { public abstract class BaseHapiFhirSystemDao<T> extends BaseHapiFhirDao<IBaseResource>implements IFhirSystemDao<T> {
@ -75,6 +76,65 @@ public abstract class BaseHapiFhirSystemDao<T> extends BaseHapiFhirDao<IBaseReso
myEntityManager.createQuery("DELETE from ResourceTag t").executeUpdate(); myEntityManager.createQuery("DELETE from ResourceTag t").executeUpdate();
} }
private int doPerformReindexingPass(final Integer theCount) {
TransactionTemplate txTemplate = new TransactionTemplate(myTxManager);
txTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRED);
return txTemplate.execute(new TransactionCallback<Integer>() {
@SuppressWarnings("unchecked")
@Override
public Integer doInTransaction(TransactionStatus theStatus) {
TypedQuery<ResourceTable> q = myEntityManager.createQuery("SELECT t FROM " + ResourceTable.class.getSimpleName() + " t WHERE t.myIndexStatus IS null", ResourceTable.class);
int maxResult = 500;
if (theCount != null) {
maxResult = Math.min(theCount, 2000);
}
q.setMaxResults(maxResult);
List<ResourceTable> resources = q.getResultList();
ourLog.info("Indexing {} resources", resources.size());
int count = 0;
long start = System.currentTimeMillis();
for (ResourceTable resourceTable : resources) {
final IBaseResource resource;
try {
resource = toResource(resourceTable);
} catch (DataFormatException e) {
ourLog.warn("Failure parsing resource: {}", e.toString());
throw new UnprocessableEntityException(Long.toString(resourceTable.getId()));
}
@SuppressWarnings("rawtypes")
final IFhirResourceDao dao = getDao(resource.getClass());
if (dao == null) {
ourLog.warn("No DAO for type: {}", resource.getClass());
throw new UnprocessableEntityException(Long.toString(resourceTable.getId()));
}
if (resource.getIdElement().isIdPartValid() == false) {
ourLog.warn("Not going to try and index an invalid ID: {}", resource.getIdElement());
throw new UnprocessableEntityException(Long.toString(resourceTable.getId()));
}
try {
dao.update(resource, null, true);
} catch (Exception e) {
ourLog.error("Failed to index resource {}: {}", new Object[] { resource.getIdElement(), e.toString() });
throw new UnprocessableEntityException(Long.toString(resourceTable.getId()));
}
count++;
}
long delay = System.currentTimeMillis() - start;
ourLog.info("Indexed {} / {} resources in {}ms", new Object[] { count, resources.size(), delay });
return resources.size();
}
});
}
@Override @Override
public TagList getAllTags() { public TagList getAllTags() {
// Notify interceptors // Notify interceptors
@ -132,85 +192,31 @@ public abstract class BaseHapiFhirSystemDao<T> extends BaseHapiFhirDao<IBaseReso
return myEntityManager.createQuery("UPDATE " + ResourceTable.class.getSimpleName() + " t SET t.myIndexStatus = null").executeUpdate(); return myEntityManager.createQuery("UPDATE " + ResourceTable.class.getSimpleName() + " t SET t.myIndexStatus = null").executeUpdate();
} }
private void markResourceAsIndexingFailed(final ResourceTable theResourceTable) { private void markResourceAsIndexingFailed(final long theId) {
ourLog.info("Marking resource with PID {} and ID {} as indexing_failed", new Object[] { theResourceTable.getId(), theResourceTable.getIdDt().getValue() });
TransactionTemplate txTemplate = new TransactionTemplate(myTxManager); TransactionTemplate txTemplate = new TransactionTemplate(myTxManager);
txTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW); txTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
txTemplate.execute(new TransactionCallback<Void>() { txTemplate.execute(new TransactionCallback<Void>() {
@Override @Override
public Void doInTransaction(TransactionStatus theStatus) { public Void doInTransaction(TransactionStatus theStatus) {
ourLog.info("Marking resource with PID {} as indexing_failed", new Object[] { theId });
Query q = myEntityManager.createQuery("UPDATE ResourceTable t SET t.myIndexStatus = :status WHERE t.myId = :id"); Query q = myEntityManager.createQuery("UPDATE ResourceTable t SET t.myIndexStatus = :status WHERE t.myId = :id");
q.setParameter("status", INDEX_STATUS_INDEXING_FAILED); q.setParameter("status", INDEX_STATUS_INDEXING_FAILED);
q.setParameter("id", theResourceTable.getId()); q.setParameter("id", theId);
q.executeUpdate(); q.executeUpdate();
return null; return null;
} }
}); });
} }
@SuppressWarnings("unchecked")
@Override @Override
@Transactional() @Transactional(propagation=Propagation.NOT_SUPPORTED)
public int performReindexingPass(Integer theCount) { public int performReindexingPass(final Integer theCount) {
TypedQuery<ResourceTable> q = myEntityManager.createQuery("SELECT t FROM " + ResourceTable.class.getSimpleName() + " t WHERE t.myIndexStatus IS null", ResourceTable.class); try {
return doPerformReindexingPass(theCount);
int maxResult = 500; } catch (UnprocessableEntityException e) {
if (theCount != null) { markResourceAsIndexingFailed(Long.parseLong(e.getMessage()));
maxResult = Math.min(theCount, 2000); return -1;
} }
q.setMaxResults(maxResult);
List<ResourceTable> resources = q.getResultList();
ourLog.info("Indexing {} resources", resources.size());
int count = 0;
long start = System.currentTimeMillis();
for (ResourceTable resourceTable : resources) {
final IBaseResource resource;
try {
resource = toResource(resourceTable);
} catch (DataFormatException e) {
ourLog.warn("Failure parsing resource: {}", e.toString());
markResourceAsIndexingFailed(resourceTable);
continue;
}
@SuppressWarnings("rawtypes")
final IFhirResourceDao dao = getDao(resource.getClass());
if (dao == null) {
ourLog.warn("No DAO for type: {}", resource.getClass());
markResourceAsIndexingFailed(resourceTable);
continue;
}
if (resource.getIdElement().isIdPartValid() == false) {
ourLog.warn("Not going to try and index an invalid ID: {}", resource.getIdElement());
markResourceAsIndexingFailed(resourceTable);
continue;
}
try {
TransactionTemplate txTemplate = new TransactionTemplate(myTxManager);
txTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
txTemplate.execute(new TransactionCallback<Void>() {
@Override
public Void doInTransaction(TransactionStatus theStatus) {
dao.update(resource, null, true);
return null;
}
});
} catch (Exception e) {
ourLog.error("Failed to index resource {}: {}", new Object[] { resource.getIdElement(), e.toString() });
markResourceAsIndexingFailed(resourceTable);
}
count++;
}
long delay = System.currentTimeMillis() - start;
ourLog.info("Indexed {} / {} resources in {}ms", new Object[] { count, resources.size(), delay });
return resources.size();
} }
protected ResourceTable tryToLoadEntity(IdDt nextId) { protected ResourceTable tryToLoadEntity(IdDt nextId) {