1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
|
/*
* Copyright (c) Xerox Corporation 1998. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linking this file statically or dynamically with other modules is making
* a combined work based on this file. Thus, the terms and conditions of
* the GNU General Public License cover the whole combination.
*
* In addition, as a special exception, the copyright holders of this file
* give you permission to combine this file with free software programs or
* libraries that are released under the GNU LGPL and with code included in
* the standard release of ns-2 under the Apache 2.0 license or under
* otherwise-compatible licenses with advertising requirements (or modified
* versions of such code, with unchanged license). You may copy and
* distribute such a system following the terms of the GNU GPL for this
* file and the licenses of the other code concerned, provided that you
* include the source code of that other code when and as the GNU GPL
* requires distribution of source code.
*
* Note that people who make modified versions of this file are not
* obligated to grant this special exception for their modified versions;
* it is their choice whether to do so. The GNU General Public License
* gives permission to release a modified version without this exception;
* this exception also makes it possible to release a modified version
* which carries forward this exception.
*
* $Header: /cvsroot/nsnam/ns-2/webcache/http.cc,v 1.22 2009/12/30 22:06:34 tom_henderson Exp $
*
*/
//
// Implementation of the HTTP agent. We want a separate agent for HTTP because
// we are interested in (detailed) HTTP headers, instead of just request and
// response patterns.
//
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdarg.h>
#include "tclcl.h"
#include "agent.h"
#include "app.h"
#include "tcp-simple.h"
#include "http.h"
#include "http-aux.h"
#include "trace.h"
#include "tcpapp.h"
#include "mcache.h"
//----------------------------------------------------------------------
// Http Application
//
// Allows multiple concurrent HTTP connections
//----------------------------------------------------------------------
static class HttpAppClass : public TclClass {
public:
HttpAppClass() : TclClass("Http") {}
TclObject* create(int, const char*const*) {
return (new HttpApp());
}
} class_http_app;
// What states should be in a http agent?
HttpApp::HttpApp() : log_(0)
{
bind("id_", &id_);
// Map a client address to a particular TCP agent
tpa_ = new Tcl_HashTable;
Tcl_InitHashTable(tpa_, TCL_ONE_WORD_KEYS);
}
HttpApp::~HttpApp()
{
if (tpa_ != NULL) {
Tcl_DeleteHashTable(tpa_);
delete tpa_;
}
}
int HttpApp::add_cnc(HttpApp* client, TcpApp *agt)
{
int newEntry = 1;
long key = client->id();
Tcl_HashEntry *he = Tcl_CreateHashEntry(tpa_,
(const char *) key,
&newEntry);
if (he == NULL)
return -1;
if (newEntry)
Tcl_SetHashValue(he, (ClientData)agt);
return 0;
}
void HttpApp::delete_cnc(HttpApp* client)
{
long key = client->id();
Tcl_HashEntry *he = Tcl_FindHashEntry(tpa_,(const char *)key);
if (he != NULL) {
TcpApp *cnc = (TcpApp *)Tcl_GetHashValue(he);
Tcl_DeleteHashEntry(he);
delete cnc;
}
}
TcpApp* HttpApp::lookup_cnc(HttpApp* client)
{
long key = client->id();
Tcl_HashEntry *he =
Tcl_FindHashEntry(tpa_, (const char *)key);
if (he == NULL)
return NULL;
return (TcpApp *)Tcl_GetHashValue(he);
}
// Basic functionalities:
int HttpApp::command(int argc, const char*const* argv)
{
Tcl& tcl = Tcl::instance();
if (argc == 2) {
if (strcmp(argv[1], "id") == 0) {
if (argc == 3) {
id_ = atoi(argv[2]);
tcl.resultf("%d", id_);
} else
tcl.resultf("%d", id_);
return TCL_OK;
} else if (strcmp(argv[1], "log") == 0) {
// Return the name of the log channel
if (log_ != NULL)
tcl.resultf("%s", Tcl_GetChannelName(log_));
else
tcl.result("");
return TCL_OK;
}
} else if (argc == 3) {
if (strcmp(argv[1], "get-modtime") == 0) {
double mt;
if (pool_->get_mtime(argv[2], mt) != -1) {
tcl.resultf("%.17g", mt);
return TCL_OK;
} else
return TCL_ERROR;
} else if (strcmp(argv[1], "exist-page") == 0) {
tcl.resultf("%d", pool_->exist_page(argv[2]));
return TCL_OK;
} else if (strcmp(argv[1], "get-size") == 0) {
int size;
if (pool_->get_size(argv[2], size) != -1) {
tcl.resultf("%d", size);
return TCL_OK;
} else
return TCL_ERROR;
} else if (strcmp(argv[1], "get-age") == 0) {
double age;
if (pool_->get_age(argv[2], age) != -1) {
tcl.resultf("%.17g", age);
return TCL_OK;
} else
return TCL_ERROR;
} else if (strcmp(argv[1], "get-cachetime") == 0) {
double et;
if (pool_->get_etime(argv[2], et) != -1) {
tcl.resultf("%.17g", et);
return TCL_OK;
} else
return TCL_ERROR;
} else if (strcmp(argv[1], "get-page") == 0) {
char buf[4096];
if (pool_->get_pageinfo(argv[2], buf) != -1) {
tcl.resultf("%s", buf);
return TCL_OK;
} else
return TCL_ERROR;
} else if (strcmp(argv[1], "get-cnc") == 0) {
/*
* <http> get-cnc <client>
*
* Given the communication party, get the tcp agent
* connected to it.
*/
HttpApp *client =
(HttpApp *)TclObject::lookup(argv[2]);
TcpApp *cnc = (TcpApp *)lookup_cnc(client);
if (cnc == NULL)
tcl.result("");
else
tcl.resultf("%s", cnc->name());
return TCL_OK;
} else if (strcmp(argv[1], "set-pagepool") == 0) {
pool_ = (ClientPagePool*)TclObject::lookup(argv[2]);
if (pool_ != NULL)
return TCL_OK;
else
return TCL_ERROR;
} else if (strcmp(argv[1], "is-connected") == 0) {
/*
* <http> is-connected <server>
*/
HttpApp *a = (HttpApp*)TclObject::lookup(argv[2]);
TcpApp *cnc = (TcpApp*)lookup_cnc(a);
if (cnc == NULL)
tcl.result("0");
else
tcl.result("1");
return TCL_OK;
} else if (strcmp(argv[1], "is-valid") == 0) {
ClientPage *pg =
(ClientPage *)pool_->get_page(argv[2]);
if (pg == NULL) {
tcl.resultf("%d is-valid: No page %s",
id_, argv[2]);
return TCL_ERROR;
}
tcl.resultf("%d", pg->is_valid());
return TCL_OK;
} else if (strcmp(argv[1], "log") == 0) {
int mode;
log_ = Tcl_GetChannel(tcl.interp(),
(char*)argv[2], &mode);
if (log_ == 0) {
tcl.resultf("%d: invalid log file handle %s\n",
id_, argv[2]);
return TCL_ERROR;
}
return TCL_OK;
} else if (strcmp(argv[1], "disconnect") == 0) {
/*
* <http> disconnect <client>
* Delete the association of source and sink TCP.
*/
HttpApp *client =
(HttpApp *)TclObject::lookup(argv[2]);
delete_cnc(client);
return TCL_OK;
} else if (strcmp(argv[1], "get-pagetype") == 0) {
/*
* <http> get-pagetype <pageid>
* return the page type
*/
ClientPage *pg =
(ClientPage*)pool_->get_page(argv[2]);
if (pg == NULL) {
tcl.resultf("%d get-pagetype: No page %s",
id_, argv[2]);
return TCL_ERROR;
}
switch (pg->type()) {
case HTML:
tcl.result("HTML");
break;
case MEDIA:
tcl.result("MEDIA");
break;
default:
fprintf(stderr, "Unknown page type %d",
pg->type());
return TCL_ERROR;
}
return TCL_OK;
} else if (strcmp(argv[1], "get-layer") == 0) {
// Assume the page is a MediaPage
MediaPage *pg = (MediaPage *)pool_->get_page(argv[2]);
if (pg == NULL) {
tcl.resultf("%d get-layer: No page %s",
id_, argv[2]);
return TCL_ERROR;
}
if (pg->type() != MEDIA) {
tcl.resultf("%d get-layer %s not a media page",
id_, argv[2]);
return TCL_ERROR;
}
tcl.resultf("%d", pg->num_layer());
return TCL_OK;
}
} else if (argc == 4) {
if (strcmp(argv[1], "connect") == 0) {
/*
* <http> connect <client> <ts>
*
* Associate a TCP agent with the given client.
* <ts> is the agent used to send packets out.
* We assume two-way TCP connection, therefore we
* only need one agent.
*/
HttpApp *client =
(HttpApp *)TclObject::lookup(argv[2]);
TcpApp *cnc = (TcpApp *)TclObject::lookup(argv[3]);
if (add_cnc(client, cnc)) {
tcl.resultf("%s: failed to connect to %s",
name_, argv[2]);
return TCL_ERROR;
}
// Set data delivery target
cnc->target() = (Process*)this;
return TCL_OK;
} else if (strcmp(argv[1], "set-modtime") == 0) {
double mt = strtod(argv[3], NULL);
if (pool_->set_mtime(argv[2], mt) != -1)
return TCL_OK;
else
return TCL_ERROR;
} else if (strcmp(argv[1], "set-cachetime") == 0) {
double et = Scheduler::instance().clock();
if (pool_->set_etime(argv[2], et) != -1)
return TCL_OK;
else
return TCL_ERROR;
}
} else {
if (strcmp(argv[1], "send") == 0) {
/*
* <http> send <client> <bytes> <callback>
*/
HttpApp *client =
(HttpApp *)TclObject::lookup(argv[2]);
if (client == NULL) {
tcl.add_errorf("%s: bad client name %s",
name_, argv[2]);
return TCL_ERROR;
}
int bytes = atoi(argv[3]);
TcpApp *cnc = (TcpApp *)lookup_cnc(client);
if (cnc == NULL) {
//tcl.resultf("%s: no connection to client %s",
// name_, argv[2]);
// Tolerate it
return TCL_OK;
}
char *buf = strdup(argv[4]);
HttpNormalData *d =
new HttpNormalData(id_, bytes, buf);
cnc->send(bytes, d);
// delete d;
free(buf);
return TCL_OK;
} else if (strcmp(argv[1], "enter-page") == 0) {
ClientPage* pg = pool_->enter_page(argc, argv);
if (pg == NULL)
return TCL_ERROR;
else
return TCL_OK;
} else if (strcmp(argv[1], "evTrace") == 0) {
char buf[1024], *p;
if (log_ != 0) {
sprintf(buf, TIME_FORMAT" i %d ",
BaseTrace::round(Scheduler::instance().clock()),
id_);
p = &(buf[strlen(buf)]);
for (int i = 2; i < argc; i++) {
strcpy(p, argv[i]);
p += strlen(argv[i]);
*(p++) = ' ';
}
// Stick in a newline.
*(p++) = '\n', *p = 0;
Tcl_Write(log_, buf, p-buf);
}
return TCL_OK;
}
}
return TclObject::command(argc, argv);
}
void HttpApp::log(const char* fmt, ...)
{
// Don't do anything if we don't have a log file.
if (log_ == 0)
return;
char buf[10240], *p;
sprintf(buf, TIME_FORMAT" i %d ",
BaseTrace::round(Scheduler::instance().clock()), id_);
p = &(buf[strlen(buf)]);
va_list ap;
va_start(ap, fmt);
vsprintf(p, fmt, ap);
Tcl_Write(log_, buf, strlen(buf));
}
void HttpApp::process_data(int, AppData* data)
{
if (data == NULL)
return;
switch (data->type()) {
case HTTP_NORMAL: {
HttpNormalData *tmp = (HttpNormalData*)data;
Tcl::instance().eval(tmp->str());
break;
}
default:
fprintf(stderr, "Bad http invalidation data type %d\n",
data->type());
abort();
break;
}
}
//----------------------------------------------------------------------
// Clients
//----------------------------------------------------------------------
static class HttpClientClass : public TclClass {
public:
HttpClientClass() : TclClass("Http/Client") {}
TclObject* create(int, const char*const*) {
return (new HttpClient());
}
} class_httpclient_app;
//----------------------------------------------------------------------
// Servers
//----------------------------------------------------------------------
static class HttpServerClass : public TclClass {
public:
HttpServerClass() : TclClass("Http/Server") {}
TclObject* create(int, const char*const*) {
return (new HttpServer());
}
} class_httpserver_app;
static class HttpInvalServerClass : public TclClass {
public:
HttpInvalServerClass() : TclClass("Http/Server/Inval") {}
TclObject* create(int, const char*const*) {
return (new HttpInvalServer());
}
} class_httpinvalserver_app;
static class HttpYucInvalServerClass : public TclClass {
public:
HttpYucInvalServerClass() : TclClass("Http/Server/Inval/Yuc") {}
TclObject* create(int, const char*const*) {
return (new HttpYucInvalServer());
}
} class_httpyucinvalserver_app;
HttpYucInvalServer::HttpYucInvalServer() :
inv_sender_(0), invlist_(0), num_inv_(0)
{
bind("hb_interval_", &hb_interval_);
bind("enable_upd_", &enable_upd_);
bind("Ca_", &Ca_);
bind("Cb_", &Cb_);
bind("push_thresh_", &push_thresh_);
bind("push_high_bound_", &push_high_bound_);
bind("push_low_bound_", &push_low_bound_);
}
int HttpYucInvalServer::command(int argc, const char*const* argv)
{
Tcl& tcl = Tcl::instance();
switch (argv[1][0]) {
case 'a':
if (strcmp(argv[1], "add-inval-sender") == 0) {
HttpUInvalAgent *tmp =
(HttpUInvalAgent *)TclObject::lookup(argv[2]);
if (tmp == NULL) {
tcl.resultf("Non-existent agent %s", argv[2]);
return TCL_ERROR;
}
inv_sender_ = tmp;
return TCL_OK;
} if (strcmp(argv[1], "add-inv") == 0) {
/*
* <server> add-inv <pageid> <modtime>
*/
double mtime = strtod(argv[3], NULL);
add_inv(argv[2], mtime);
return TCL_OK;
}
break;
case 'c':
if (strcmp(argv[1], "count-request") == 0) {
ClientPage *pg =
(ClientPage *)pool_->get_page(argv[2]);
if (pg == NULL) {
tcl.resultf("%d count-request: No page %s",
id_, argv[2]);
return TCL_ERROR;
}
pg->count_request(Cb_, push_high_bound_);
log("S NTF p %s v %d\n", argv[2], pg->counter());
return TCL_OK;
} else if (strcmp(argv[1], "count-inval") == 0) {
ClientPage *pg =
(ClientPage *)pool_->get_page(argv[2]);
if (pg == NULL) {
tcl.resultf("%d count-inval: No page %s",
id_, argv[2]);
return TCL_ERROR;
}
pg->count_inval(Ca_, push_low_bound_);
log("S NTF p %s v %d\n", argv[2], pg->counter());
return TCL_OK;
}
break;
case 'i':
if (strcmp(argv[1], "is-pushable") == 0) {
ClientPage *pg =
(ClientPage *)pool_->get_page(argv[2]);
if (pg == NULL) {
tcl.resultf("%d is-pushable: No page %s",
id_, argv[2]);
return TCL_ERROR;
}
if (pg->is_mpush() &&
(Scheduler::instance().clock() - pg->mpush_time() >
HTTP_HBEXPIRE_COUNT*hb_interval_)) {
// If mandatory push timer expires, stop push
pg->clear_mpush();
fprintf(stderr,
"server %d timeout mpush\n", id_);
}
tcl.resultf("%d", ((enable_upd_ &&
(pg->counter() >= push_thresh_)) ||
pg->is_mpush()));
return TCL_OK;
}
break;
case 'r':
if ((strcmp(argv[1], "request-mpush") == 0) ||
(strcmp(argv[1], "refresh-mpush") == 0)) {
ClientPage *pg =
(ClientPage *)pool_->get_page(argv[2]);
if (pg == NULL) {
tcl.resultf("%d is-valid: No page %s",
id_, argv[2]);
return TCL_ERROR;
}
pg->set_mpush(Scheduler::instance().clock());
return TCL_OK;
}
break;
case 's':
if (strcmp(argv[1], "send-hb") == 0) {
send_heartbeat();
return TCL_OK;
} else if (strcmp(argv[1], "stop-mpush") == 0) {
ClientPage *pg =
(ClientPage *)pool_->get_page(argv[2]);
if (pg == NULL) {
tcl.resultf("%d is-valid: No page %s",
id_, argv[2]);
return TCL_ERROR;
}
pg->clear_mpush();
fprintf(stderr, "server %d stopped mpush\n", id_);
return TCL_OK;
}
break;
}
return HttpApp::command(argc, argv);
}
void HttpYucInvalServer::add_inv(const char *name, double mtime)
{
InvalidationRec *p = get_invrec(name);
if ((p != NULL) && (p->mtime() < mtime)) {
p->detach();
delete p;
p = NULL;
num_inv_--;
}
if (p == NULL) {
p = new InvalidationRec(name, mtime);
p->insert(&invlist_);
num_inv_++;
}
}
InvalidationRec* HttpYucInvalServer::get_invrec(const char *name)
{
// XXX What should we do if we already have an
// invalidation record of this page in our
// invlist_? --> We should replace it with the new one
InvalidationRec *r = invlist_;
for (r = invlist_; r != NULL; r = r->next())
if (strcmp(name, r->pg()) == 0)
return r;
return NULL;
}
HttpHbData* HttpYucInvalServer::pack_heartbeat()
{
HttpHbData *data = new HttpHbData(id_, num_inv_);
InvalidationRec *p = invlist_, *q;
int i = 0;
while (p != NULL) {
data->add(i++, p);
// Clearing up invalidation sending list
if (!p->dec_scount()) {
// Each invalidation is sent to its children
// for at most HTTP_HBEXPIRE times. After that
// the invalidation record is removed from
// the list
q = p;
p = p->next();
q->detach();
delete q;
num_inv_--;
} else
p = p->next();
}
return data;
}
void HttpYucInvalServer::send_hb_helper(int size, AppData *data)
{
inv_sender_->send(size, data);
}
void HttpYucInvalServer::send_heartbeat()
{
if (inv_sender_ == NULL)
return;
HttpHbData* d = pack_heartbeat();
send_hb_helper(d->cost(), d);
}
//----------------------------------------------------------------------
// Http cache with invalidation protocols. Http/Cache and Http/Cache/Inval
// are used as base classes and provide common TCL methods. Http/Cache
// derives Http/Cache/TTL and Http/Cache/TTL/Old. Http/Cache/Inval derives
// unicast invalidation and multicast invalidation.
//----------------------------------------------------------------------
static class HttpCacheClass : public TclClass {
public:
HttpCacheClass() : TclClass("Http/Cache") {}
TclObject* create(int, const char*const*) {
return (new HttpCache());
}
} class_httpcache_app;
static class HttpInvalCacheClass : public TclClass {
public:
HttpInvalCacheClass() : TclClass("Http/Cache/Inval") {}
TclObject* create(int, const char*const*) {
return (new HttpInvalCache());
}
} class_httpinvalcache_app;
static class HttpMInvalCacheClass : public TclClass {
public:
HttpMInvalCacheClass() : TclClass("Http/Cache/Inval/Mcast") {}
TclObject* create(int, const char*const*) {
return (new HttpMInvalCache());
}
} class_HttpMInvalCache_app;
// Static members and functions
HttpMInvalCache** HttpMInvalCache::CacheRepository_ = NULL;
int HttpMInvalCache::NumCache_ = 0;
void HttpMInvalCache::add_cache(HttpMInvalCache *c)
{
if (CacheRepository_ == NULL) {
CacheRepository_ = new HttpMInvalCache* [c->id() + 1];
CacheRepository_[c->id()] = c;
NumCache_ = c->id();
} else if (NumCache_ < c->id()) {
HttpMInvalCache** p = new HttpMInvalCache* [c->id()+1];
memcpy(p, CacheRepository_,
(c->id()+1)*sizeof(HttpMInvalCache*));
delete[]CacheRepository_;
CacheRepository_ = p;
NumCache_ = c->id();
p[c->id()] = c;
} else
CacheRepository_[c->id()] = c;
}
HttpMInvalCache::HttpMInvalCache() :
hb_timer_(this, HTTP_HBINTERVAL),
inv_sender_(0), num_sender_(0), size_sender_(0),
invlist_(0), num_inv_(0), inv_parent_(NULL),
upd_sender_(NULL), num_updater_(0), size_updater_(0)
{
bind("hb_interval_", &hb_interval_);
bind("enable_upd_", &enable_upd_); // If we allow push
bind("Ca_", &Ca_);
bind("Cb_", &Cb_);
bind("push_thresh_", &push_thresh_);
bind("push_high_bound_", &push_high_bound_);
bind("push_low_bound_", &push_low_bound_);
hb_timer_.set_interval(hb_interval_);
Tcl_InitHashTable(&sstate_, TCL_ONE_WORD_KEYS);
Tcl_InitHashTable(&nbr_, TCL_ONE_WORD_KEYS);
}
HttpMInvalCache::~HttpMInvalCache()
{
if (num_sender_ > 0)
delete []inv_sender_;
Tcl_DeleteHashTable(&sstate_);
Tcl_DeleteHashTable(&nbr_);
}
int HttpMInvalCache::command(int argc, const char*const* argv)
{
Tcl& tcl = Tcl::instance();
if (argc < 2)
return HttpInvalCache::command(argc, argv);
switch (argv[1][0]) {
case 'a':
if ((strcmp(argv[1], "add-inval-listener") == 0) ||
(strcmp(argv[1], "add-upd-listener") == 0)) {
HttpInvalAgent *tmp =
(HttpInvalAgent *)TclObject::lookup(argv[2]);
tmp->attachApp((Application *)this);
return TCL_OK;
} else if (strcmp(argv[1], "add-inval-sender") == 0) {
HttpInvalAgent *tmp =
(HttpInvalAgent *)TclObject::lookup(argv[2]);
if (tmp == NULL) {
tcl.resultf("Non-existent agent %s", argv[2]);
return TCL_ERROR;
}
if (num_sender_ == size_sender_) {
HttpInvalAgent **tt =
new HttpInvalAgent*[size_sender_+5];
memcpy(tt, inv_sender_,
sizeof(HttpInvalAgent*)*size_sender_);
delete []inv_sender_;
size_sender_ += 5;
inv_sender_ = tt;
}
inv_sender_[num_sender_++] = tmp;
return TCL_OK;
} else if (strcmp(argv[1], "add-to-map") == 0) {
add_cache(this);
return TCL_OK;
} else if (strcmp(argv[1], "add-upd-sender") == 0) {
HttpInvalAgent *tmp =
(HttpInvalAgent *)TclObject::lookup(argv[2]);
if (tmp == NULL) {
tcl.resultf("Non-existent agent %s", argv[2]);
return TCL_ERROR;
}
if (num_updater_ == size_updater_) {
HttpInvalAgent **tt =
new HttpInvalAgent*[size_updater_+5];
memcpy(tt, upd_sender_,
sizeof(HttpInvalAgent*)*size_updater_);
delete []upd_sender_;
size_updater_ += 5;
upd_sender_ = tt;
}
upd_sender_[num_updater_++] = tmp;
return TCL_OK;
}
break;
case 'c':
if (strcmp(argv[1], "count-request") == 0) {
ClientPage *pg =
(ClientPage *)pool_->get_page(argv[2]);
if (pg == NULL) {
tcl.resultf("%d count-request: No page %s",
id_, argv[2]);
return TCL_ERROR;
}
pg->count_request(Cb_, push_high_bound_);
log("E NTF p %s v %d\n", argv[2], pg->counter());
return TCL_OK;
} else if (strcmp(argv[1], "check-sstate") == 0) {
/*
* <cache> check-sstate <sid> <cid>
* If server is re-connected, reinstate it
*/
int sid = atoi(argv[2]);
int cid = atoi(argv[3]);
check_sstate(sid, cid);
return TCL_OK;
}
break;
case 'i':
// XXX We don't need a "is-pushable" for cache!
if (strcmp(argv[1], "is-unread") == 0) {
ClientPage *pg =
(ClientPage *)pool_->get_page(argv[2]);
if (pg == NULL) {
tcl.resultf("%d is-unread: No page %s",
id_, argv[2]);
return TCL_ERROR;
}
tcl.resultf("%d", pg->is_unread());
return TCL_OK;
}
break;
case 'j':
if (strcmp(argv[1], "join") == 0) {
/*
* <cache> join <server_id> <cache>
*
* <server> join via <cache>. If they are the same,
* it means we are the primary cache for <server>.
*/
int sid = atoi(argv[2]);
HttpMInvalCache *cache =
(HttpMInvalCache*)TclObject::lookup(argv[3]);
if (cache == NULL) {
tcl.add_errorf("Non-existent cache %s", argv[3]);
return TCL_ERROR;
}
// Add neighbor cache if necessary
NeighborCache *c = lookup_nbr(cache->id());
if (c == NULL)
add_nbr(cache);
// Establish server invalidation contract
check_sstate(sid, cache->id());
return TCL_OK;
}
break;
case 'p':
if (strcmp(argv[1], "parent-cache") == 0) {
/*
* <cache> parent-cache <web_server_id>
* Return the parent cache of <web_server_id> in the
* virtual distribution tree.
*/
int sid = atoi(argv[2]);
SState *sst = lookup_sstate(sid);
if (sst == NULL)
tcl.result("");
else {
// Bad hack... :(
NeighborCache *c = lookup_nbr(sst->cache()->cache()->id());
tcl.resultf("%s", c->cache()->name());
}
return TCL_OK;
} else if (strcmp(argv[1], "push-children") == 0) {
// Multicast the pushed page to all children
ClientPage *pg =
(ClientPage *)pool_->get_page(argv[2]);
if (pg == NULL) {
tcl.resultf("%d is-valid: No page %s",
id_, argv[2]);
return TCL_ERROR;
}
send_upd(pg);
return TCL_OK;
}
break;
case 'r':
if (strcmp(argv[1], "recv-inv") == 0) {
/*
* <cache> recv-inv <pageid> <modtime>
* This should be called only by a web server,
* therefore we do not check the validity of the
* invalidation
*/
// Pack it into a HttpHbData, and process it
HttpHbData *d = new HttpHbData(id_, 1);
strcpy(d->rec_pg(0), argv[2]);
d->rec_mtime(0) = strtod(argv[3], NULL);
//int old_inv = num_inv_;
tcl.resultf("%d", recv_inv(d));
delete d;
return TCL_OK;
} else if (strcmp(argv[1], "recv-push") == 0) {
/*
* <cache> recv-push <pageid> args
*/
HttpUpdateData *d = new HttpUpdateData(id_, 1);
strcpy(d->rec_page(0), argv[2]);
for (int i = 3; i < argc; i+=2) {
if (strcmp(argv[i], "modtime") == 0)
d->rec_mtime(0) = strtod(argv[i+1], NULL);
else if (strcmp(argv[i], "size") == 0) {
d->rec_size(0) = atoi(argv[i+1]);
// XXX need to set total update page size
d->set_pgsize(d->rec_size(0));
} else if (strcmp(argv[i], "age") == 0)
d->rec_age(0) = strtod(argv[i+1], NULL);
}
tcl.resultf("%d", recv_upd(d));
delete d;
return TCL_OK;
} else if (strcmp(argv[1], "register-server") == 0) {
/*
* <self> register-server <cache_id> <server_id>
* We get a GET response about a page from <server>,
* which we hear from <cache>
*/
int cid = atoi(argv[2]);
int sid = atoi(argv[3]);
// Assuming we've already known the cache
check_sstate(sid, cid);
return TCL_OK;
}
break;
case 's':
if (strcmp(argv[1], "start-hbtimer") == 0) {
if (hb_timer_.status() == TIMER_IDLE)
hb_timer_.sched();
return TCL_OK;
} else if (strcmp(argv[1], "server-hb") == 0) {
int id = atoi(argv[2]);
recv_heartbeat(id);
return TCL_OK;
} else if (strcmp(argv[1], "set-pinv-agent") == 0) {
inv_parent_ =
(HttpUInvalAgent*)TclObject::lookup(argv[2]);
return TCL_OK;
} else if (strcmp(argv[1], "set-parent") == 0) {
HttpMInvalCache *c =
(HttpMInvalCache*)TclObject::lookup(argv[2]);
if (c == NULL) {
tcl.add_errorf("Non-existent cache %s", argv[2]);
return TCL_ERROR;
}
// Add parent cache into known cache list
add_nbr(c);
return TCL_OK;
} else if (strcmp(argv[1], "set-unread") == 0) {
ClientPage *pg =
(ClientPage *)pool_->get_page(argv[2]);
if (pg == NULL) {
tcl.resultf("%d is-valid: No page %s",
id_, argv[2]);
return TCL_ERROR;
}
pg->set_unread();
return TCL_OK;
} else if (strcmp(argv[1], "set-read") == 0) {
ClientPage *pg =
(ClientPage *)pool_->get_page(argv[2]);
if (pg == NULL) {
tcl.resultf("%d is-valid: No page %s",
id_, argv[2]);
return TCL_ERROR;
}
pg->set_read();
return TCL_OK;
} else if (strcmp(argv[1], "set-mandatory-push") == 0) {
ClientPage *pg =
(ClientPage *)pool_->get_page(argv[2]);
if (pg == NULL) {
tcl.resultf("%d is-valid: No page %s",
id_, argv[2]);
return TCL_ERROR;
}
pg->set_mpush(Scheduler::instance().clock());
return TCL_OK;
} else if (strcmp(argv[1], "stop-mpush") == 0) {
ClientPage *pg =
(ClientPage *)pool_->get_page(argv[2]);
if (pg == NULL) {
tcl.resultf("%d is-valid: No page %s",
id_, argv[2]);
return TCL_ERROR;
}
pg->clear_mpush();
return TCL_OK;
}
break;
default:
break;
}
return HttpInvalCache::command(argc, argv);
}
void HttpMInvalCache::check_sstate(int sid, int cid)
{
if ((sid == cid) && (cid == id_))
// How come?
return;
SState *sst = lookup_sstate(sid);
NeighborCache *c = lookup_nbr(cid);
if (sst == NULL) {
if (c == NULL) {
fprintf(stderr,
"%g: cache %d: No neighbor cache for received invalidation from %d via %d\n",
Scheduler::instance().clock(), id_, sid, cid);
abort();
}
#ifdef WEBCACHE_DEBUG
fprintf(stderr,
"%g: cache %d: registered server %d via cache %d\n",
Scheduler::instance().clock(), id_, sid, cid);
#endif
sst = new SState(c);
add_sstate(sid, sst);
c->add_server(sid);
} else if (sst->is_down()) {
sst->up();
if (cid != id_) {
if (c == NULL) {
fprintf(stderr,
"[%g]: Cache %d has an invalid neighbor cache %d\n",
Scheduler::instance().clock(), id_, cid);
abort();
}
c->server_up(sid);
}
#ifdef WEBCACHE_DEBUG
fprintf(stderr,
"[%g] Cache %d reconnected to server %d via cache %d\n",
Scheduler::instance().clock(), id_,
sid, cid);
#endif
Tcl::instance().evalf("%s mark-rejoin", name_);
}
}
void HttpMInvalCache::add_sstate(int sid, SState *sst)
{
int newEntry = 1;
long key = sid;
Tcl_HashEntry *he =
Tcl_CreateHashEntry(&sstate_, (const char *)key, &newEntry);
if (he == NULL)
return;
if (newEntry)
Tcl_SetHashValue(he, (ClientData)sst);
}
HttpMInvalCache::SState* HttpMInvalCache::lookup_sstate(int sid)
{
long key = sid;
Tcl_HashEntry *he = Tcl_FindHashEntry(&sstate_, (const char *)key);
if (he == NULL)
return NULL;
return (SState *)Tcl_GetHashValue(he);
}
NeighborCache* HttpMInvalCache::lookup_nbr(int id)
{
long key = id;
Tcl_HashEntry *he = Tcl_FindHashEntry(&nbr_, (const char *)key);
if (he == NULL)
return NULL;
return (NeighborCache *)Tcl_GetHashValue(he);
}
// Add a new neighbor cache
void HttpMInvalCache::add_nbr(HttpMInvalCache *cache)
{
int newEntry = 1;
long key = cache->id ();
Tcl_HashEntry *he =
Tcl_CreateHashEntry(&nbr_, (const char *)key,
&newEntry);
if (he == NULL)
return;
// If this cache already exists, don't do anything
if (!newEntry)
return;
// Start a timer for the neighbor
LivenessTimer *timer =
new LivenessTimer(this,HTTP_HBEXPIRE_COUNT*hb_interval_,
cache->id());
double time = Scheduler::instance().clock();
NeighborCache *c = new NeighborCache(cache, time, timer);
Tcl_SetHashValue(he, (ClientData)c);
}
// Two ways to receive a heartbeat: (1) via HttpInvalAgent; (2) via TCP
// connection between a server and a primary cache. (See "server-hb" handling
// in command().
void HttpMInvalCache::recv_heartbeat(int id)
{
// Receive time of the heartbeat
double time = Scheduler::instance().clock();
NeighborCache *c = lookup_nbr(id);
if (c == NULL) {
// XXX
// The only possible place for this to happen is in the TLC
// group, where no JOIN could ever reach. Moreover,
// we don't even have an entry for that cache yet, so here
// we add that cache into our entry, and later on we'll add
// corresponding servers there.
if (id == id_)
return;
add_nbr(map_cache(id));
#ifdef WEBCACHE_DEBUG
fprintf(stderr, "TLC %d discovered TLC %d\n", id_, id);
#endif
return;
} else if (c->is_down()) {
// Neighbor cache recovers. Don't do anything special and
// let invalid entries recover themselves
c->up();
#ifdef WEBCACHE_DEBUG
fprintf(stderr, "[%g] Cache %d reconnected to cache %d\n",
Scheduler::instance().clock(), id_, id);
#endif
Tcl::instance().evalf("%s mark-rejoin", name_);
} else
// Update heartbeat time
c->reset_timer(time);
}
void HttpMInvalCache::invalidate_server(int sid)
{
SState *sst = lookup_sstate(sid);
if (sst->is_down())
// If this server is already marked down, return
return;
sst->down();
pool_->invalidate_server(sid);
}
void HttpMInvalCache::handle_node_failure(int cid)
{
#ifdef WEBCACHE_DEBUG
fprintf(stderr, "[%g] Cache %d disconnected from cache %d\n",
Scheduler::instance().clock(), id_, cid);
#endif
Tcl::instance().evalf("%s mark-leave", name_);
NeighborCache *c = lookup_nbr(cid);
if (c == NULL) {
fprintf(stderr, "%s: An unknown neighbor cache %d failed.\n",
name_, cid);
}
// Mark the cache down
c->down();
// Invalidate entries of all servers related to that cache
// XXX We don't have an iterator for all servers in NeighborCache!
c->invalidate(this);
// Send leave message to all children
HttpLeaveData* data = new HttpLeaveData(id_, c->num());
c->pack_leave(*data);
send_leave(data);
}
void HttpMInvalCache::recv_leave(HttpLeaveData *d)
{
#ifdef WEBCACHE_DEBUG
fprintf(stderr, "[%g] Cache %d gets a LEAVE from cache %d\n",
Scheduler::instance().clock(), id_, d->id());
#endif
if (d->num() == 0) {
fprintf(stderr,
"%s (%g) gets a leave from cache without server!\n",
name_, Scheduler::instance().clock());
return;
}
SState *sst;
HttpLeaveData* data = new HttpLeaveData(id_, d->num());
NeighborCache *c = lookup_nbr(d->id());
int i, j;
for (i = 0, j = 0; i < d->num(); i++) {
sst = lookup_sstate(d->rec_id(i));
// If we haven't heard of that server, which means we don't
// have any page of that server, ignore the leave message.
if (sst == NULL)
continue;
// If it's already marked down, don't bother again.
if (sst->is_down())
continue;
// If we hear a LEAVE about a server from one of
// our child in the virtual distribution tree
// of the server, ignore it.
if (c != sst->cache())
continue;
// We have the page, and we hold inval contract. Invalidate
// the page and inform our children of it.
sst->down();
data->add(j++, d->rec_id(i));
pool_->invalidate_server(d->rec_id(i));
Tcl::instance().evalf("%s mark-leave", name_);
}
// Delete it if it's not sent out
if (j > 0)
send_leave(data);
delete data;
}
void HttpMInvalCache::send_leave(HttpLeaveData *d)
{
send_hb_helper(d->cost(), d);
}
void HttpMInvalCache::timeout(int reason)
{
switch (reason) {
case HTTP_INVALIDATION:
// Send an invalidation message
send_heartbeat();
break;
case HTTP_UPDATE:
// XXX do nothing. May put client selective joining update
// group here.
break;
default:
fprintf(stderr, "%s: Unknown reason %d", name_, reason);
break;
}
}
void HttpMInvalCache::process_data(int size, AppData* data)
{
if (data == NULL)
return;
switch (data->type()) {
case HTTP_INVALIDATION: {
// Update timer for the source of the heartbeat
HttpHbData *inv = (HttpHbData*)data;
recv_heartbeat(inv->id());
recv_inv(inv);
break;
}
case HTTP_UPDATE: {
// Replace all updated pages
HttpUpdateData *pg = (HttpUpdateData*)data;
recv_upd(pg);
break;
}
// JOIN messages are sent via TCP and direct TCL callback.
case HTTP_LEAVE: {
HttpLeaveData *l = (HttpLeaveData*)data;
recv_leave(l);
break;
}
default:
HttpApp::process_data(size, data);
return;
}
}
void HttpMInvalCache::add_inv(const char *name, double mtime)
{
InvalidationRec *p = get_invrec(name);
if ((p != NULL) && (p->mtime() < mtime)) {
p->detach();
delete p;
p = NULL;
num_inv_--;
}
if (p == NULL) {
p = new InvalidationRec(name, mtime);
p->insert(&invlist_);
num_inv_++;
}
}
InvalidationRec* HttpMInvalCache::get_invrec(const char *name)
{
// XXX What should we do if we already have an
// invalidation record of this page in our
// invlist_? --> We should replace it with the new one
InvalidationRec *r = invlist_;
for (r = invlist_; r != NULL; r = r->next())
if (strcmp(name, r->pg()) == 0)
return r;
return NULL;
}
HttpHbData* HttpMInvalCache::pack_heartbeat()
{
HttpHbData *data = new HttpHbData(id_, num_inv_);
InvalidationRec *p = invlist_, *q;
int i = 0;
while (p != NULL) {
data->add(i++, p);
// Clearing up invalidation sending list
if (!p->dec_scount()) {
// Each invalidation is sent to its children
// for at most HTTP_HBEXPIRE times. After that
// the invalidation record is removed from
// the list
q = p;
p = p->next();
q->detach();
delete q;
num_inv_--;
} else
p = p->next();
}
return data;
}
int HttpMInvalCache::recv_inv(HttpHbData *data)
{
if (data->num_inv() == 0)
return 0;
InvalidationRec *head;
data->extract(head);
int old_inv = num_inv_;
process_inv(data->num_inv(), head, data->id());
//log("E GINV z %d\n", data->size());
if (old_inv < num_inv_)
// This invalidation is valid
return 1;
else
return 0;
}
// Get an invalidation, check invalidation modtimes, then setup
// invalidation forwarding entries
// The input invalidation record list is destroyed.
void HttpMInvalCache::process_inv(int, InvalidationRec *ivlist, int cache)
{
InvalidationRec *p = ivlist, *q, *r;
//int upd = 0;
while (p != NULL) {
ClientPage* pg = (ClientPage *)pool_->get_page(p->pg());
// XXX Establish server states. Server states only gets
// established when we have a page (no matter if we have its
// content), and we have got an invalidation for the page.
// Then we know we've got an invalidation contract for the
// page.
if (pg != NULL) {
check_sstate(pg->server()->id(), cache);
// Count this invalidation no matter whether we're
// going to drop it. But if we doesn't get it
// from our virtual parent, don't count it
SState *sst = lookup_sstate(pg->server()->id());
if (sst == NULL) {
// How come we doesn't know the server???
fprintf(stderr,
"%s %d: couldn't find the server.\n",
__FILE__, __LINE__);
abort();
}
if ((sst->cache()->cache()->id() == cache) &&
(pg->mtime() > p->mtime())) {
// Don't count repeated invalidations.
pg->count_inval(Ca_, push_low_bound_);
log("E NTF p %s v %d\n",p->pg(),pg->counter());
}
}
// Hook for filters of derived classes
if (recv_inv_filter(pg, p) == HTTP_INVALCACHE_FILTERED) {
// If we do not have the page, or we have (or know
// about) a newer page, ignore this invalidation
// record and keep going.
//
// If we have this version of the page, and it's
// already invalid, ignore this extra invalidation
q = p;
p = p->next();
q->detach();
delete q;
} else {
// Otherwise we invalidate our page and setup a
// invalidation sending record for the page
pg->invalidate(p->mtime());
// Delete existing record for that page if any
q = get_invrec(p->pg());
if ((q != NULL) && (q->mtime() < p->mtime())) {
q->detach();
delete q;
q = NULL;
num_inv_--;
}
r = p;
p = p->next();
r->detach();
// Insert it if necessary
if (q == NULL) {
r->insert(&invlist_);
num_inv_++;
// XXX
Tcl::instance().evalf("%s mark-invalid",name_);
log("E GINV p %s m %.17g\n", r->pg(), r->mtime());
} else
delete r;
}
}
}
void HttpMInvalCache::send_hb_helper(int size, AppData *data)
{
if (inv_parent_ != NULL)
inv_parent_->send(size, data->copy());
for (int i = 0; i < num_sender_; i++)
inv_sender_[i]->send(size, data->copy());
}
void HttpMInvalCache::send_heartbeat()
{
if ((num_sender_ == 0) && (inv_parent_ == NULL))
return;
HttpHbData* d = pack_heartbeat();
send_hb_helper(d->cost(), d);
delete d;
}
int HttpMInvalCache::recv_upd(HttpUpdateData *d)
{
if (d->num() != 1) {
fprintf(stderr,
"%d gets an update which contain !=1 pages.\n", id_);
abort();
}
ClientPage *pg = pool_->get_page(d->rec_page(0));
if (pg != NULL)
{
if (pg->mtime() >= d->rec_mtime(0)) {
// If we've already had this version, or a newer
// version, ignore this old push
// fprintf(stderr, "[%g] %d gets an old push\n",
// Scheduler::instance().clock(), id_);
// log("E OLD m %g p %g\n", d->rec_mtime(0), pg->mtime());
return 0;
} else {
// Our old page is invalidated by this new push,
// set up invalidation records for our children
add_inv(d->rec_page(0), d->rec_mtime(0));
pg->count_inval(Ca_, push_low_bound_);
log("E NTF p %s v %d\n", d->rec_page(0),pg->counter());
}
}
// Add the new page into our pool
ClientPage *q = pool_->enter_page(d->rec_page(0), d->rec_size(0),
d->rec_mtime(0),
Scheduler::instance().clock(),
d->rec_age(0));
// By default the page is valid and read. Set it as unread
q->set_unread();
log("E GUPD m %.17g z %d\n", d->rec_mtime(0), d->pgsize());
Tcl::instance().evalf("%s mark-valid", name_);
// XXX If the page was previously marked as MandatoryPush, then
// we need to check if it's timed out
if (q->is_mpush() && (Scheduler::instance().clock() - q->mpush_time()
> HTTP_HBEXPIRE_COUNT*hb_interval_)) {
// If mandatory push timer expires, stop push
q->clear_mpush();
Tcl::instance().evalf("%s cancel-mpush-refresh %s",
name_, d->rec_page(0));
}
if ((enable_upd_ && (q->counter() >= push_thresh_)) || q->is_mpush())
{
// XXX Continue pushing if we either select to push, or
// were instructed to do so.
return 1;
}
else
{
return 0;
}
}
HttpUpdateData* HttpMInvalCache::pack_upd(ClientPage* page)
{
HttpUpdateData *data = new HttpUpdateData(id_, 1);
data->add(0, page);
return data;
}
void HttpMInvalCache::send_upd_helper(int pgsize, AppData* data)
{
for (int i = 0; i < num_updater_; i++)
upd_sender_[i]->send(pgsize, data->copy());
}
void HttpMInvalCache::send_upd(ClientPage *page)
{
if ((num_updater_ == 0) || !enable_upd_)
return;
HttpUpdateData* d = pack_upd(page);
send_upd_helper(d->pgsize(), d);
delete d;
}
//----------------------------------------------------------------------
// Multicast invalidation + two way liveness messages +
// invalidation filtering.
//----------------------------------------------------------------------
static class HttpPercInvalCacheClass : public TclClass {
public:
HttpPercInvalCacheClass() : TclClass("Http/Cache/Inval/Mcast/Perc") {}
TclObject* create(int, const char*const*) {
return (new HttpPercInvalCache());
}
} class_HttpPercInvalCache_app;
HttpPercInvalCache::HttpPercInvalCache()
{
bind("direct_request_", &direct_request_);
}
int HttpPercInvalCache::command(int argc, const char*const* argv)
{
Tcl& tcl = Tcl::instance();
if (strcmp(argv[1], "is-header-valid") == 0) {
ClientPage *pg =
(ClientPage *)pool_->get_page(argv[2]);
if (pg == NULL) {
tcl.resultf("%d is-valid: No page %s",
id_, argv[2]);
return TCL_ERROR;
}
tcl.resultf("%d", pg->is_header_valid());
return TCL_OK;
} else if (strcmp(argv[1], "enter-metadata") == 0) {
/*
* <cache> enter-metadata <args...>
* The same arguments as enter-page, but set the page status
* as HTTP_VALID_HEADER, i.e., if we get a request, we need
* to fetch the actual valid page content
*/
ClientPage *pg = pool_->enter_metadata(argc, argv);
if (pg == NULL)
return TCL_ERROR;
else
return TCL_OK;
}
return HttpMInvalCache::command(argc, argv);
}
|