@@ -73,7 +73,7 @@ public String readUserId(String sessionId) {
7373 final File f = getUserDataFileForSession (sessionId );
7474 if (!f .exists () || f .length () == 0 ) {
7575 Logger .getLogger ().d ("No userId set for session " + sessionId );
76- safeDeleteCorruptFile (f );
76+ safeDeleteCorruptFile (f , "No userId set for session " + sessionId );
7777 return null ;
7878 }
7979
@@ -85,7 +85,7 @@ public String readUserId(String sessionId) {
8585 return userId ;
8686 } catch (Exception e ) {
8787 Logger .getLogger ().w ("Error deserializing user metadata." , e );
88- safeDeleteCorruptFile (f );
88+ safeDeleteCorruptFile (f , "Error deserializing user metadata: " + e );
8989 } finally {
9090 CommonUtils .closeOrLog (is , "Failed to close user metadata file." );
9191 }
@@ -107,7 +107,7 @@ public void writeKeyData(String sessionId, Map<String, String> keyData, boolean
107107 writer .flush ();
108108 } catch (Exception e ) {
109109 Logger .getLogger ().w ("Error serializing key/value metadata." , e );
110- safeDeleteCorruptFile (f );
110+ safeDeleteCorruptFile (f , "Error serializing key/value metadata." + e );
111111 } finally {
112112 CommonUtils .closeOrLog (writer , "Failed to close key/value metadata file." );
113113 }
@@ -121,7 +121,19 @@ Map<String, String> readKeyData(String sessionId, boolean isInternal) {
121121 final File f =
122122 isInternal ? getInternalKeysFileForSession (sessionId ) : getKeysFileForSession (sessionId );
123123 if (!f .exists () || f .length () == 0 ) {
124- safeDeleteCorruptFile (f );
124+ if (!f .exists ()) {
125+ safeDeleteCorruptFile (
126+ f ,
127+ String .format (
128+ "The file at path \" %s\" doesn't exists for session \" %s\" " ,
129+ f .getAbsolutePath (), sessionId ));
130+ } else {
131+ safeDeleteCorruptFile (
132+ f ,
133+ String .format (
134+ "The file at path \" %s\" has a length of zero for session \" %s\" " ,
135+ f .getAbsolutePath (), sessionId ));
136+ }
125137 return Collections .emptyMap ();
126138 }
127139
@@ -131,7 +143,7 @@ Map<String, String> readKeyData(String sessionId, boolean isInternal) {
131143 return jsonToKeysData (CommonUtils .streamToString (is ));
132144 } catch (Exception e ) {
133145 Logger .getLogger ().w ("Error deserializing user metadata." , e );
134- safeDeleteCorruptFile (f );
146+ safeDeleteCorruptFile (f , "Error deserializing user metadata." + e );
135147 } finally {
136148 CommonUtils .closeOrLog (is , "Failed to close user metadata file." );
137149 }
@@ -141,7 +153,20 @@ Map<String, String> readKeyData(String sessionId, boolean isInternal) {
141153 public List <RolloutAssignment > readRolloutsState (String sessionId ) {
142154 final File f = getRolloutsStateForSession (sessionId );
143155 if (!f .exists () || f .length () == 0 ) {
144- safeDeleteCorruptFile (f );
156+ if (!f .exists ()) {
157+ safeDeleteCorruptFile (
158+ f ,
159+ String .format (
160+ "The file at path \" %s\" doesn't exists for session \" %s\" " ,
161+ f .getAbsolutePath (), sessionId ));
162+ } else {
163+ safeDeleteCorruptFile (
164+ f ,
165+ String .format (
166+ "The file at path \" %s\" has a length of zero for session \" %s\" " ,
167+ f .getAbsolutePath (), sessionId ));
168+ }
169+
145170 return Collections .emptyList ();
146171 }
147172
@@ -154,7 +179,7 @@ public List<RolloutAssignment> readRolloutsState(String sessionId) {
154179 return rolloutsState ;
155180 } catch (Exception e ) {
156181 Logger .getLogger ().w ("Error deserializing rollouts state." , e );
157- safeDeleteCorruptFile (f );
182+ safeDeleteCorruptFile (f , "Error deserializing rollouts state." + e );
158183 } finally {
159184 CommonUtils .closeOrLog (is , "Failed to close rollouts state file." );
160185 }
@@ -164,7 +189,7 @@ public List<RolloutAssignment> readRolloutsState(String sessionId) {
164189 public void writeRolloutState (String sessionId , List <RolloutAssignment > rolloutsState ) {
165190 final File f = getRolloutsStateForSession (sessionId );
166191 if (rolloutsState .isEmpty ()) {
167- safeDeleteCorruptFile (f );
192+ safeDeleteCorruptFile (f , "Rollout state is empty for session: " + sessionId );
168193 return ;
169194 }
170195
@@ -176,7 +201,7 @@ public void writeRolloutState(String sessionId, List<RolloutAssignment> rollouts
176201 writer .flush ();
177202 } catch (Exception e ) {
178203 Logger .getLogger ().w ("Error serializing rollouts state." , e );
179- safeDeleteCorruptFile (f );
204+ safeDeleteCorruptFile (f , "Error serializing rollouts state." + e );
180205 } finally {
181206 CommonUtils .closeOrLog (writer , "Failed to close rollouts state file." );
182207 }
@@ -270,9 +295,13 @@ private static String valueOrNull(JSONObject json, String key) {
270295 return !json .isNull (key ) ? json .optString (key , null ) : null ;
271296 }
272297
273- private static void safeDeleteCorruptFile (File file ) {
298+ private static void safeDeleteCorruptFile (File file , String reason ) {
274299 if (file .exists () && file .delete ()) {
275- Logger .getLogger ().i ("Deleted corrupt file: " + file .getAbsolutePath ());
300+
301+ Logger .getLogger ()
302+ .i (
303+ String .format (
304+ "Deleted corrupt file due to \" %s\" : %s" , reason , file .getAbsolutePath ()));
276305 }
277306 }
278307}
0 commit comments