Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
Distributed File System
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tmp1u19
Distributed File System
Commits
67868ba6
Commit
67868ba6
authored
4 years ago
by
tmp1u19
Browse files
Options
Downloads
Patches
Plain Diff
Add the functionality for List and store in the controller
parent
c7292a34
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Controller.java
+80
-32
80 additions, 32 deletions
src/Controller.java
src/Dstore.java
+21
-10
21 additions, 10 deletions
src/Dstore.java
with
101 additions
and
42 deletions
src/Controller.java
+
80
−
32
View file @
67868ba6
import
java.io.*
;
import
java.io.*
;
import
java.net.*
;
import
java.net.*
;
import
java.nio.charset.StandardCharsets
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.zip.InflaterInputStream
;
public
class
Controller
{
public
class
Controller
{
...
@@ -12,13 +14,20 @@ public class Controller {
...
@@ -12,13 +14,20 @@ public class Controller {
// it doesn't serve any client request until at least R Dstores have joined the system
// it doesn't serve any client request until at least R Dstores have joined the system
static
File
file
=
new
File
(
"output.txt"
);
static
File
file
=
new
File
(
"output.txt"
);
static
int
R
;
// list of all dstores
static
List
<
Socket
>
ports
=
new
ArrayList
<>();
// file, list of dstores
static
HashMap
<
String
,
List
<
Socket
>>
index
=
new
HashMap
<>();
// file, status (e.g. in progress)
static
HashMap
<
String
,
String
>
status
=
new
HashMap
<>();
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
// port to listen on
// port to listen on
final
int
cport
=
Integer
.
parseInt
(
args
[
0
]);
final
int
cport
=
Integer
.
parseInt
(
args
[
0
]);
// replication factor => number of Dstores to join
// replication factor => number of Dstores to join
int
R
=
Integer
.
parseInt
(
args
[
1
]);
R
=
Integer
.
parseInt
(
args
[
1
]);
// timeout in milliseconds
// timeout in milliseconds
int
timeout
=
Integer
.
parseInt
(
args
[
2
]);
int
timeout
=
Integer
.
parseInt
(
args
[
2
]);
// how long to wait to start the next rebalance operation
// how long to wait to start the next rebalance operation
...
@@ -26,53 +35,39 @@ public class Controller {
...
@@ -26,53 +35,39 @@ public class Controller {
System
.
out
.
println
(
"Started"
);
System
.
out
.
println
(
"Started"
);
// list of all dstores
List
<
Socket
>
ports
=
new
ArrayList
<>();
// file, list of dstores
HashMap
<
String
,
List
<
Socket
>>
index
=
new
HashMap
<>();
// file, status (e.g. in progress)
HashMap
<
String
,
String
>
status
=
new
HashMap
<>();
createLogFile
();
createLogFile
();
try
{
try
{
ServerSocket
socket
=
new
ServerSocket
(
cport
);
ServerSocket
socket
=
new
ServerSocket
(
cport
);
for
(;;)
{
for
(;;)
{
try
{
try
{
if
(
R
>
0
)
{
while
(
R
>
0
)
{
// accept Dstores
System
.
out
.
println
(
"waiting for Dstore to join"
);
System
.
out
.
println
(
"waiting for Dstore to join"
);
Socket
client
=
socket
.
accept
();
// establish a connection between client and server
Socket
client
=
socket
.
accept
();
// establish a connection between client and server
System
.
out
.
print
(
R
+
": "
);
System
.
out
.
print
ln
(
R
);
ports
.
add
(
client
);
ports
.
add
(
client
);
R
=
R
-
1
;
R
=
R
-
1
;
}
else
{
//Socket clientDstore = socket.accept();
for
(
Socket
client
:
ports
)
{
Thread
t
=
new
Thread
()
{
@Override
public
void
run
()
{
try
{
handleDstores
(
client
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
};
t
.
start
();
}
break
;
}
}
// create a Thread for each client
Socket
client
=
socket
.
accept
();
Thread
t
=
new
Thread
()
{
@Override
public
void
run
()
{
try
{
handleClient
(
client
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
};
t
.
start
();
}
catch
(
Exception
e1
)
{
}
catch
(
Exception
e1
)
{
System
.
out
.
println
(
e1
);
System
.
out
.
println
(
e1
);
}
}
}
}
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
System
.
out
.
println
(
"error"
+
e
);
System
.
out
.
println
(
"error"
+
e
);
}
}
...
@@ -125,17 +120,70 @@ public class Controller {
...
@@ -125,17 +120,70 @@ public class Controller {
if
(
cmd
.
equals
(
"STORE"
))
{
if
(
cmd
.
equals
(
"STORE"
))
{
String
filename
=
tokens
[
1
];
int
filesize
=
Integer
.
parseInt
(
tokens
[
2
]);
status
.
put
(
filename
,
"store in progress"
);
log
(
"store in progress"
);
String
msg
=
""
;
for
(
Socket
socket
:
ports
)
{
msg
=
socket
.
getPort
()
+
msg
+
" "
;
}
// test and see if this actually gets all the ports
System
.
out
.
println
(
"STORE_TO "
+
msg
);
// send this message to the client so the client knows where to store the file
outputStream
.
write
((
"STORE_TO "
+
msg
).
getBytes
());
outputStream
.
flush
();
final
int
[]
num
=
{
0
};
for
(
Socket
socket
:
ports
)
{
Thread
t
=
new
Thread
()
{
@Override
public
void
run
()
{
try
{
InputStream
inputStream
=
socket
.
getInputStream
();
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
inputStream
));
String
line
=
reader
.
readLine
();
String
[]
ts
=
line
.
split
(
" "
);
String
cmd
=
ts
[
0
];
if
(
cmd
==
"STORE_ACK"
)
{
num
[
0
]++;
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
};
t
.
start
();
}
status
.
put
(
filename
,
"store complete"
);
outputStream
.
write
(
"STORE_COMPLETE"
.
getBytes
());
}
else
if
(
cmd
.
equals
(
"LOAD"
))
{
}
else
if
(
cmd
.
equals
(
"LOAD"
))
{
}
else
if
(
cmd
.
equals
(
"REMOVE"
))
{
}
else
if
(
cmd
.
equals
(
"REMOVE"
))
{
}
else
if
(
cmd
.
equals
(
"LIST"
))
{
}
else
if
(
cmd
.
equals
(
"LIST"
))
{
String
msg
=
""
;
for
(
String
filename
:
status
.
keySet
())
{
msg
=
filename
+
" "
+
msg
;
}
System
.
out
.
println
(
msg
);
outputStream
.
write
((
"LIST "
+
msg
).
getBytes
());
}
else
{
}
else
{
String
msg
=
"unknown command "
+
line
;
String
msg
=
"unknown command "
+
line
;
log
(
msg
);
log
(
msg
);
}
}
}
}
clientSocket
.
close
();
}
}
private
static
void
createLogFile
()
{
private
static
void
createLogFile
()
{
...
@@ -154,7 +202,7 @@ public class Controller {
...
@@ -154,7 +202,7 @@ public class Controller {
try
{
try
{
FileWriter
writer
=
new
FileWriter
(
file
,
true
);
FileWriter
writer
=
new
FileWriter
(
file
,
true
);
writer
.
write
(
java
.
time
.
LocalDate
.
now
()
+
", "
+
writer
.
write
(
java
.
time
.
LocalDate
.
now
()
+
", "
+
java
.
time
.
LocalTime
.
now
()
+
": "
+
message
+
'\n'
);
java
.
time
.
LocalTime
.
now
()
+
"
, CONTROLLER
: "
+
message
+
'\n'
);
writer
.
close
();
writer
.
close
();
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
...
...
This diff is collapsed.
Click to expand it.
src/Dstore.java
+
21
−
10
View file @
67868ba6
import
java.io.*
;
import
java.io.*
;
import
java.net.*
;
import
java.net.*
;
import
java.nio.charset.StandardCharsets
;
public
class
Dstore
{
public
class
Dstore
{
...
@@ -71,14 +70,17 @@ public class Dstore {
...
@@ -71,14 +70,17 @@ public class Dstore {
}
else
if
(
cmd
.
equals
(
"LOAD_DATA"
))
{
}
else
if
(
cmd
.
equals
(
"LOAD_DATA"
))
{
String
filename
=
tokens
[
1
];
String
filename
=
tokens
[
1
];
FileInputStream
file
=
new
FileInputStream
(
file_folder
+
"/"
+
filename
);
File
file
=
new
File
(
file_folder
+
"/"
+
filename
);
int
n
;
if
(
file
.
exists
())
{
while
((
n
=
file
.
read
())!=
-
1
)
{
FileInputStream
in
=
new
FileInputStream
(
file_folder
+
"/"
+
filename
);
outputStream
.
write
(
n
);
int
n
;
while
((
n
=
in
.
read
())!=
-
1
)
{
outputStream
.
write
(
n
);
}
in
.
close
();
}
else
{
clientSocket
.
close
();
}
}
file
.
close
();
}
else
if
(
cmd
.
equals
(
"LIST"
))
{
}
else
if
(
cmd
.
equals
(
"QUIT"
))
{
}
else
if
(
cmd
.
equals
(
"QUIT"
))
{
clientSocket
.
close
();
clientSocket
.
close
();
...
@@ -96,7 +98,7 @@ public class Dstore {
...
@@ -96,7 +98,7 @@ public class Dstore {
}
}
}
}
private
static
void
handleController
(
Socket
controllerSocket
)
throws
IOException
{
private
static
void
handleController
(
Socket
controllerSocket
,
String
file_folder
)
throws
IOException
{
OutputStream
outputStream
=
controllerSocket
.
getOutputStream
();
OutputStream
outputStream
=
controllerSocket
.
getOutputStream
();
InputStream
inputStream
=
controllerSocket
.
getInputStream
();
InputStream
inputStream
=
controllerSocket
.
getInputStream
();
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
inputStream
));
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
inputStream
));
...
@@ -108,6 +110,15 @@ public class Dstore {
...
@@ -108,6 +110,15 @@ public class Dstore {
if
(
cmd
.
equals
(
"REMOVE"
))
{
if
(
cmd
.
equals
(
"REMOVE"
))
{
String
filename
=
tokens
[
1
];
File
file
=
new
File
(
file_folder
+
"/"
+
filename
);
if
(
file
.
exists
())
{
file
.
delete
();
outputStream
.
write
((
"REMOVE_ACK "
+
filename
).
getBytes
());
}
else
{
outputStream
.
write
((
"ERROR_FILE_DOES_NOT_EXIST "
+
filename
).
getBytes
());
}
}
else
if
(
cmd
.
equals
(
"QUIT"
))
{
}
else
if
(
cmd
.
equals
(
"QUIT"
))
{
controllerSocket
.
close
();
controllerSocket
.
close
();
break
;
break
;
...
@@ -139,7 +150,7 @@ public class Dstore {
...
@@ -139,7 +150,7 @@ public class Dstore {
try
{
try
{
FileWriter
writer
=
new
FileWriter
(
file
,
true
);
FileWriter
writer
=
new
FileWriter
(
file
,
true
);
writer
.
write
(
java
.
time
.
LocalDate
.
now
()
+
", "
+
writer
.
write
(
java
.
time
.
LocalDate
.
now
()
+
", "
+
java
.
time
.
LocalTime
.
now
()
+
": "
+
message
+
'\n'
);
java
.
time
.
LocalTime
.
now
()
+
"
, DSTORE
: "
+
message
+
'\n'
);
writer
.
close
();
writer
.
close
();
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment