Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #SC-1193 read data from registry if es fails #45

Open
wants to merge 10 commits into
base: release-2.2.0-prime
Choose a base branch
from
13 changes: 11 additions & 2 deletions user-org-actor/src/main/java/org/sunbird/user/UserReadActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import org.sunbird.response.Response;
import org.sunbird.user.dao.IUserESDao;
import org.sunbird.user.dao.UserDaoFactory;
import org.sunbird.util.LoggerEnum;
import org.sunbird.user.service.IUserService;
import org.sunbird.user.service.UserServiceImpl;
import org.sunbird.util.ProjectLogger;
import org.sunbird.util.jsonkey.JsonKey;

Expand All @@ -28,6 +29,7 @@
)
public class UserReadActor extends BaseActor {

private IUserService userService = null;
IUserESDao userESDao = (IUserESDao) UserDaoFactory.getDaoImpl(DaoImplType.ES.getType());


Expand All @@ -48,7 +50,14 @@ public void onReceive(Request request) throws Throwable {
*/
public void readUserById(Request request) throws BaseException {
startTrace("readUserById");
Response response = userESDao.getUserById((String) request.getRequest().get(JsonKey.USER_ID));
Response response = null;
try {
response = userESDao.getUserById((String) request.getRequest().get(JsonKey.USER_ID));
} catch (Exception e) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add logger in catch block to print sacktrace, to know what went wrong.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

ProjectLogger.log("Exception occurred while reading user ES.", e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger parretn need to be same for all places, we are following "className:methodName message,LoggerEum.Value.name"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove ProjectLogger.log("Exception occurred while reading user ES.", e); because we already have logger above.

userService = new UserServiceImpl();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use service factory , no need to create instance every places,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created factory for service object creation.

response = userService.readUser(request);
}
endTrace("readUserById");
sender().tell(response, self());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.sunbird.user.dao;

import com.fasterxml.jackson.databind.JsonNode;
import org.sunbird.exception.BaseException;
import org.sunbird.exception.ProjectCommonException;
import org.sunbird.exception.message.Localizer;
Expand All @@ -23,5 +24,13 @@ public interface IUserOSDao {
*/
Response createUser(Map<String, Object> user) throws BaseException;

/**
* this method will read a user from open saber
* @param userId
* @return response
* @throws BaseException
*/
public Response readUser(JsonNode userId) throws BaseException;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why requested userId is JsonNode , it should be String

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method attribute name incorrect, here we constructed json node with valid input to read method, now attribute name changed



}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.sunbird.user.dao;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.opensaber.registry.helper.RegistryHelper;
import org.sunbird.Application;
import org.sunbird.exception.BaseException;
Expand Down Expand Up @@ -61,4 +64,23 @@ public Response createUser(Map<String, Object> user) throws BaseException {
throw new ProjectCommonException.ServerError(IResponseMessage.INTERNAL_ERROR, localizer.getMessage(IResponseMessage.INTERNAL_ERROR, null), ResponseCode.SERVER_ERROR.getCode());
}
}

/**
* this method is used to read user record from OS.
* @param inputNode
* @return response
* @throws BaseException
*/
@Override
public Response readUser(JsonNode inputNode) throws BaseException {
Response response = new Response();
try {
JsonNode responseNode = registryHelper.readEntity(inputNode,"");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use overide method for readEntity with one and two argument .

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should be at user-registry, i hope here it is not necessary.

response.putAll(objectMapper.convertValue(responseNode,Map.class));
return response;
} catch (Exception e) {
ProjectLogger.log("Exception occurred while reading user from open saber.", e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use same logging format

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

throw new ProjectCommonException.ServerError(IResponseMessage.INTERNAL_ERROR, localizer.getMessage(IResponseMessage.INTERNAL_ERROR, null), ResponseCode.SERVER_ERROR.getCode());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ public interface IUserService {
* @return Response
*/
Response createUser(Request request) throws BaseException;

/**
* This method will read user.
* @param request
* @return Response
*/
Response readUser(Request request) throws BaseException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import akka.actor.ActorRef;
import akka.pattern.Patterns;
import akka.util.Timeout;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.opensaber.registry.model.DBConnectionInfoMgr;
import org.apache.commons.lang3.StringUtils;
import org.sunbird.Application;
import org.sunbird.DaoImplType;
Expand Down Expand Up @@ -48,4 +51,16 @@ private Response saveUserAttributes(Request request) {
}
return response;
}

@Override
public Response readUser(Request request) throws BaseException {
ObjectNode idNode = JsonNodeFactory.instance.objectNode();
ObjectNode userNode = JsonNodeFactory.instance.objectNode();
indrajra marked this conversation as resolved.
Show resolved Hide resolved
String recordId = (String) request.getRequest().get(JsonKey.USER_ID);
DBConnectionInfoMgr dBConnectionInfoMgr = Application.applicationContext.getBean(DBConnectionInfoMgr.class);
idNode.put(dBConnectionInfoMgr.getUuidPropertyName(), recordId);
userNode.set("User", idNode);
IUserOSDao userDao = (IUserOSDao) UserDaoFactory.getDaoImpl(DaoImplType.OS.getType());
return userDao.readUser(userNode);
}
}